node.js AWS Lambda 函数写入 S3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40188287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AWS Lambda function write to S3
提问by KVISH
I have a Node 4.3 Lambda function in AWS. I want to be able to write a text file to S3 and have read many tutorials about how to integrate with S3. However, all of them are about how to call Lambda functions after writing to S3.
我在 AWS 中有一个 Node 4.3 Lambda 函数。我希望能够将文本文件写入 S3,并阅读了许多有关如何与 S3 集成的教程。但是,所有这些都是关于如何在写入 S3后调用 Lambda 函数。
How can I create a text file in S3 from Lambda using node? Is this possible? Amazons documentation doesn't seem to cover it.
如何使用节点从 Lambda 在 S3 中创建文本文件?这可能吗?亚马逊文档似乎没有涵盖它。
回答by Xavier Hutchinson
Yes it is absolutely possible!
是的,这是绝对可能的!
var AWS = require('aws-sdk');
function putObjectToS3(bucket, key, data){
var s3 = new AWS.S3();
var params = {
Bucket : bucket,
Key : key,
Body : data
}
s3.putObject(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
Make sure that you give your Lambda function the required write permissions to the target s3 bucket / key path by selecting or updating the IAM Role your lambda executes under.
通过选择或更新执行 lambda 的 IAM 角色,确保为 Lambda 函数授予目标 s3 存储桶/密钥路径所需的写入权限。
IAM Statement to add:
要添加的 IAM 声明:
{
"Sid": "Stmt1468366974000",
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::my-bucket-name-goes-here/optional-path-before-allow/*"
]
}
Further reading:
进一步阅读:
- AWS JavaScript SDK
- The specific "Put Object" details
- AWS JavaScript 开发工具包
- 具体“放置对象”详情
回答by Anish Agarwal
You can upload file on s3 using
您可以使用 s3 上传文件
aws-sdk
aws-sdk
If you are using IAMuser then you have to provide access key and secret key and make sure you have provided necessary permission to IAMuser.
如果您使用的是IAM用户,那么您必须提供访问密钥和秘密密钥,并确保您已向IAM用户提供了必要的权限。
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: "ACCESS_KEY",secretAccessKey: 'SECRET_KEY'});
var s3bucket = new AWS.S3({params: {Bucket: 'BUCKET_NAME'}});
function uploadFileOnS3(fileName, fileData){
var params = {
Key: fileName,
Body: fileData,
};
s3bucket.upload(params, function (err, res) {
if(err)
console.log("Error in uploading file on s3 due to "+ err)
else
console.log("File successfully uploaded.")
});
}
Here I temporarily hard-coded AWS access and secret key for testing purposes. For best practices refer to the documentation.
在这里,我临时硬编码了 AWS 访问和密钥以进行测试。有关最佳实践,请参阅文档。
回答by Steffen
IAM Statement for serverless.com - Write to S3 to specific bucket
serverless.com 的 IAM 语句 - 将 S3 写入特定存储桶
service: YOURSERVICENAME
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-west-1
timeout: 60
iamRoleStatements:
- Effect: "Allow"
Action:
- s3:PutObject
Resource: "**BUCKETARN**/*"
- Effect: "Deny"
Action:
- s3:DeleteObject
Resource: "arn:aws:s3:::**BUCKETARN**/*"
回答by Elad
After long long time of silence-failing of 'Task timed out after X' without any good error message, i went back to the beginning, to Amazon default template example, and that worked!
经过长时间的“任务在 X 后超时”的沉默失败而没有任何好的错误消息,我回到了开头,回到亚马逊默认模板示例,并且奏效了!
> Lambda > Functions > Create function > Use a blueprints > filter: s3.
> Lambda > 函数 > 创建函数 > 使用蓝图 > 过滤器:s3。
Here is my tweaked version of amazon example:
这是我的亚马逊示例的调整版本:
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
async function uploadFileOnS3(fileData, fileName) => {
const params = {
Bucket: "The-bucket-name-you-want-to-save-the-file-to",
Key: fileName,
Body: JSON.stringify(fileData),
};
try {
const response = await s3.upload(params).promise();
console.log('Response: ', response);
return response;
} catch (err) {
console.log(err);
}
};

