我正在尝试编写一个lambda脚本,它可以从站点中提取图像并将其存储在S3中。我遇到的问题是将Body属性传递给S3.putObject
方法的对象是什么类型。在文档here中,它说它应该是new Buffer('...') || 'STRING_VALUE' || streamObject
,但我不确定如何将https响应转换为其中之一。这是我尝试过的:
var AWS = require('aws-sdk');
var https = require('https');
var Readable = require('stream').Readable;
var s3 = new AWS.S3();
var fs = require('fs');
var url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/AmazonWebservices_Logo.svg/500px-AmazonWebservices_Logo.svg.png';
exports.handler = function(event, context) {
https.get(url, function(response) {
var params = {
Bucket: 'example',
Key: 'aws-logo.png',
Body: response // fs.createReadStream(response); doesn't work, arg should be a path to a file...
// just putting response errors out with "Cannot determine length of [object Object]"
};
s3.putObject(params, function(err, data) {
if (err) {
console.error(err, err.stack);
} else {
console.log(data);
}
});
});
};
答案 0 :(得分:5)
如评论中所示,Lambda允许在/tmp
中保存文件。但你真的不需要它......
response
不包含文件内容,但包含http响应(包含其状态代码和标题)。
您可以尝试这样的事情:
var AWS = require('aws-sdk');
var https = require('https');
var s3 = new AWS.S3();
var url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/AmazonWebservices_Logo.svg/500px-AmazonWebservices_Logo.svg.png';
exports.handler = function(event, context) {
https.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
// Agregates chunks
body += chunk;
});
res.on('end', function() {
// Once you received all chunks, send to S3
var params = {
Bucket: 'example',
Key: 'aws-logo.png',
Body: body
};
s3.putObject(params, function(err, data) {
if (err) {
console.error(err, err.stack);
} else {
console.log(data);
}
});
});
});
};
答案 1 :(得分:3)
试试这个包https://www.npmjs.com/package/request
var request = require('request');
exports.handler = function (event, context) {
s3.putObject({
Bucket: 'example',
Key: 'aws-logo.png',
Body: request.get(url, {followRedirect: false})
}, function (err, data) {
if (err) console.error(err, err.stack);
else console.log(data);
})
}