我对亚马逊网络服务非常陌生,我正在尝试创建一个文件,并使用以下代码将其上传到S3:
let uploadCsvToS3 = (fileData, csvName) => {
let data = fileData;
let readObj = new Readable();
let timeOfErrorLog = timeUtility.convertDateToUnixTS(new Date());
let errorLogFileName = `errorlog/errorlog_${timeOfErrorLog}_${csvName}`;
readObj.push(data); // Push the CSV
readObj.push(null); // Signal that we're done writing the CSV
var upload = s3Stream.upload({
"Bucket": "bucketname",
"Key": errorLogFileName
});
readObj.pipe(upload);
upload.on('error', function (error) {
console.log('error is:',error);
console.log(`There was an error uploading error file: ${errorLogFileName} on S3`);
});
upload.on('uploaded', function (details) {
console.log(`Successfully Uploaded error log file on S3 by name: ${errorLogFileName}`);
console.log(details);
markErrorLogAsDumped(csvName);
});
}
当我在本地运行它时,它运行良好,但是当我尝试通过AWS lambda执行该操作时,它会抛出此错误:
Failed to create a multipart upload on S3:
{
"message": "Access Denied",
"code": "AccessDenied",
"region": null,
"time": "2019-01-14T10:21:01.983Z",
"requestId": "24FC75B2103C1FC4",
"extendedRequestId": "iTDnNrMWSfixL9j6S6yDz68AgTIZthUlTzjZ/Rwrqu7CUJj5f4lrq2Ds7hFapbvzko3DYyRGA/E=",
"statusCode": 403,
"retryable": false,
"retryDelay": 64.85863274563219
}
可能是什么问题?
答案 0 :(得分:1)
它可能会在您的本地测试中使用,因为它将使用您的AWS凭证。您需要确保由Lambda担任的角色具有对目标S3位置的写入权限。
例如:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1547468052811",
"Action": "s3:*",
"Effect": "Allow",
"Resource": "arn:aws:s3:::<bucket_name>/"
}
]
}