[英]aws lambda write a file on s3 - javascript
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
exports.handler = async (event) => {
var bucketName = 'arn:aws:s3:::alessio77';
var keyName = 'prova.txt';
var content = 'This is a sample text file';
var params = { 'Bucket': bucketName, 'Key': keyName, 'Body': content };
s3.putObject(params, function (err, data) {
console.log('entrato')
if (err)
console.log(err)
else
console.log("Successfully saved object to " + bucketName + "/" + keyName);
});
};
this code neither write a file nor give me an error this is the log: 此代码既不写文件也不给我一个错误,这是日志:
START RequestId: 7c93b1b9-73c1-4f18-9824-095bcbe292bf Version: $LATEST END RequestId: 7c93b1b9-73c1-4f18-9824-095bcbe292bf REPORT RequestId: 7c93b1b9-73c1-4f18-9824-095bcbe292bf Duration: 706.18 ms Billed Duration: 800 ms Memory Size: 128 MB Max Memory Used: 90 MB START RequestId:7c93b1b9-73c1-4f18-9824-095bcbe292bf版本:$ LATEST END RequestId:7c93b1b9-73c1-4f18-9824-095bcbe292bf REPORT RequestId:7c93b1b9-73c1-4f18-9824-095bcbe292bf持续时间:706.18 ms计费大小:128 MB使用的最大内存:90 MB
The s3.putObject is async and you need to wait for it. s3.putObject是异步的,您需要等待。 Most all aws api calls returns an AWS.Request which can return a promise.
几乎所有的aws api调用都会返回一个AWS.Request,它可以返回一个Promise。 Here is a solution using await.
这是使用等待的解决方案。
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
exports.handler = async (event) => {
var bucketName = 'arn:aws:s3:::alessio77';
var keyName = 'prova.txt';
var content = 'This is a sample text file';
var params = { 'Bucket': bucketName, 'Key': keyName, 'Body': content };
try {
console.log('entrato')
const data = await s3.putObject(params).promise();
console.log("Successfully saved object to " + bucketName + "/" + keyName);
} catch (err) {
console.log(err)
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.