Bre*_*eds 6 amazon-s3 amazon-web-services node.js sendgrid aws-lambda
美好的一天.
我有一个简单的问题:如何将图像从S3存储桶下载到Lambda函数临时文件夹进行处理?基本上,我需要将它附加到一封电子邮件中(我在本地测试时可以这样做).
我试过了:
s3.download_file(bucket, key, '/tmp/image.png')
Run Code Online (Sandbox Code Playgroud)
以及(不确定哪些参数可以帮助我完成工作):
s3.getObject(params, (err, data) => {
if (err) {
console.log(err);
const message = `Error getting object ${key} from bucket ${bucket}.`;
console.log(message);
callback(message);
} else {
console.log('CONTENT TYPE:', data.ContentType);
callback(null, data.ContentType);
}
});
Run Code Online (Sandbox Code Playgroud)
就像我说的,简单的问题,由于某种原因,我无法找到解决方案.
谢谢!
如果您直接将其写入文件系统,您也可以使用流来完成。它可能会更快/对内存更友好,尤其是在像 Lambda 这样的内存受限环境中。
var fs = require('fs');
var path = require('path');
var params = {
Bucket: "mybucket",
Key: "image.png"
};
var tempFileName = path.join('/tmp', 'downloadedimage.png');
var tempFile = fs.createWriteStream(tempFileName);
s3.getObject(params).createReadStream().pipe(tempFile);
Run Code Online (Sandbox Code Playgroud)
您可以使用aws s3 api获取图像,然后使用fs将其写入tmp文件夹.
var params = { Bucket: "BUCKET_NAME", Key: "OBJECT_KEY" };
s3.getObject(params, function(err, data){ if (err) {
console.error(err.code, "-", err.message);
return callback(err); }
fs.writeFile('/tmp/filename', data.Body, function(err){
if(err)
console.log(err.code, "-", err.message);
return callback(err);
});
});
Run Code Online (Sandbox Code Playgroud)
出于好奇,为什么你需要写文件才能附加它?将文件写入磁盘似乎有点多余,以便您可以从磁盘读取它
| 归档时间: |
|
| 查看次数: |
11864 次 |
| 最近记录: |