0

我正在尝试将文件从 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 模块,那也很好。

4

1 回答 1

2

我不确定如何使它与 request.js 一起工作,但本机 HTTP 模块似乎可以工作。

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();
于 2015-07-28T22:58:28.223 回答