从request.js到s3.upload的管道导致零字节文件

时间:2015-07-28 02:12:43

标签: node.js amazon-s3 aws-sdk requestjs

我正在尝试将request.js中的文件直接传递给s3。下面的代码是我设法弄清楚的最接近的代码。它运行,但最终在s3上的文件是零字节。我错了什么?

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

如果使用原生的http / https模块更容易,那也没关系。

1 个答案:

答案 0 :(得分:2)

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