简体   繁体   中英

using request.get file body is not correct

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.

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',
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM