Piping from request.js to s3.upload results in a zero byte file

泪湿孤枕 提交于 2019-12-11 19:34:13

问题


I'm trying to pipe a file from request.js straight to s3. The code below is the closest I've managed to figure out. It runs, but the file that ends up on s3 is zero bytes. What am I getting wrong?

var request = require('request');
var AWS = require('aws-sdk');
var s3 = new AWS.S3();

request('https://placekitten.com/g/2000/2000')
    .on('response', function(response) {
        response.on('data', function(data) {
            // this is getting called
            console.log(data.length);
        });

        s3.upload({
            Body: response,
            Bucket: 'myBucket'
            Key: 'foo.jpg',
            // if ContentLength is set, the request just seems to hang?
            // ContentLength: parseInt(response.headers['content-length']),
        }, function(err, data) {
            // no error, but zero byte file
            console.log(err, data);
        });
    });

If there's an easier way using the native http/https modules, that would be fine too.


回答1:


I'm not sure how to make it work with request.js, but the native HTTP modules seem to work.

var AWS = require('aws-sdk');
var https = require('https');
var s3 = new AWS.S3();

https.request('https://placekitten.com/g/2000/2000', function(response) {
    s3.upload({
        Body: response,
        Bucket: 'myBucket',
        Key: 'foo.jpg',
    }, function(err, data) {
        console.log(err, data);
    });
}).end();


来源:https://stackoverflow.com/questions/31666314/piping-from-request-js-to-s3-upload-results-in-a-zero-byte-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!