0

I'm trying to upload a file to s3 using request.js but the file data seems to be incorrect after I upload it. Should I be using a data property of the response object instead?

        var flickrPhotoUrl = 'http://c1.staticflickr.com/3/2207/2305523141_1f98981685_z.jpg?zz=1';

        request.get(flickrPhotoUrl, function(error, response, body){

            if (body) {
                s3.upload({
                    Body: body,
                    Bucket: 'my-uploads',
                    Key: 'photo.jpg',
                }, function (err) {
                    done(err);
                });
            }
            else
            {
                done('no response');
            }
        });

When I grab the file from s3 after the upload, it's not a recognizable image and seems to be twice as big.

MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

1 Answers1

1

request by default converts your image binary data to utf8 string. That's why the file size is larger than the actual size. Try passing encoding:null to keep the body as buffer:

request.get({encoding:null, uri: flickrPhotoUrl}, function(error, response, body){ 
  ...
})

Update: I think you can also pass a readable stream in Body parameter. This is faster than above for large files.

var stream = request.get(flickrPhotoUrl)
s3.upload({
    Body: stream,
    Bucket: 'my-uploads',
    Key: 'photo.jpg',
}
hassansin
  • 16,918
  • 3
  • 43
  • 49