node.js - 使用 NodeJS 和请求模块以及 aws-sdk 将图像从 Parse 移动到 S3/CloudFront

标签 node.js amazon-web-services amazon-s3 request

好的。 NodeJS 使用请求模块。在 Parse(在 S3 中)中下载资源,我想使用 aws-sdk Node 模块上传到我的 S3 存储桶(在 CloudFront 端点后面)。这是我的代码:

var AWS = require('aws-sdk');
var request = require('request');

AWS.config.loadFromPath('./aws-config.json');
var s3 = new AWS.S3();

var url = "http://files.parse.com/[the rest of the url]";

request(url, function (error, response, body) {
    console.log(response);
    if (!error && response.statusCode == 200) {
        s3.putObject({
            "Body": body,
            "Key": "thumbnail2.jpg",
            "Bucket": "[my-bucket]"
        }, function (error, data) {
            console.log(error || data);
        });
    }
});

如果我打开解析 url,我会看到图像。如果我打开我存储桶中的 url,我会看到一个损坏的图像。

最佳答案

你需要做两件事:

  • 设置请求的编码为二进制
  • 将二进制缓冲区传递给S3

试一试:

            var options = {
                uri: "http://files.parse.com/[the rest of the url]",
                encoding: 'binary',
            };

            request(options, function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    s3.putObject({
                        "Body": new Buffer(body, 'binary'),
                        "Key": "thumbnail2.jpg",
                        "Bucket": "[my-bucket]"
                    }, function (error, data) {
                        console.log(error || data);
                    });
                }
            });

关于node.js - 使用 NodeJS 和请求模块以及 aws-sdk 将图像从 Parse 移动到 S3/CloudFront,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20229456/

相关文章:

python - 使用 Python boto3 从 S3 读取 JSON 文件

amazon-web-services - 带有子目录的 S3 API 网关代理

node.js - MongoDB CursorNotFound 在 collection.find() 上出现数百个小记录错误

javascript - 如何在给定元素值的情况下为 DOM 元素获取唯一的 CSS 选择器?

amazon-web-services - 带有Docker和SQS的AWS Beanstalk上的Sinatra应用

amazon-web-services - 从 AWS EC2 元数据链接本地地址获取多个项目

python - 使用 Flask 和 Boto3 从 HTML 表单上传文件

java - JUnit AWSCredentials NoClassDefFoundError with LocalStackContainer

javascript - VueJS 中的模型 URL 参数

javascript - res.render 不加载新页面