Christopher Holmok
Christopher Holmok

Reputation: 91

Using NodeJS and request module and aws-sdk to move images from Parse to S3/CloudFront

Okay. NodeJS using the request module. Downloading a resource in Parse (which is in S3) and I want upload to my S3 bucket (behind a CloudFront endpoint) using the aws-sdk node module. Here is my code:

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);
        });
    }
});

If I open the parse url I see the image. If I open the url that is in my bucket, I get a broken image.

Upvotes: 3

Views: 846

Answers (1)

paulcfx
paulcfx

Reputation: 41

There are 2 things you need to do:

  • set request's encoding to binary
  • pass in binary buffer to S3

Give this a try:

            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);
                    });
                }
            });

Upvotes: 4

Related Questions