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