我正在为lambda函数使用默认代码:
console.log('Loading function');
var aws = require('aws-sdk');
var s3 = new aws.S3({ apiVersion: '2006-03-01' });
exports.handler = function(event, context) {
//console.log('Received event:', JSON.stringify(event, null, 2));
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
var params = {
Bucket: bucket,
Key: key
};
s3.getObject(params, function(err, data) {
if (err) {
console.log(err);
var message = "Error getting object " + key + " from bucket " + bucket +
". Make sure they exist and your bucket is in the same region as this function.";
console.log(message);
context.fail(message);
} else {
console.log('CONTENT TYPE:', data.ContentType);
context.succeed(data.ContentType);
}
});
};
但是我得到一个访问被拒绝的错误:
2016-02-24T14:21:21.503Z kvyo1midvc2r69gm Loading function
START RequestId: baf9049b-db01-11e5-bc34-791df91353a9 Version: $LATEST
2016-02-24T14:21:22.500Z baf9049b-db01-11e5-bc34-791df91353a9 { [AccessDenied: Access Denied] message: 'Access Denied', code: 'AccessDenied', region: null, time: Wed Feb 24 2016 14:21:22 GMT+0000 (UTC), requestId: '215CD9BB4094E209', extendedRequestId: '0kDBEyMiJYbMApEqJuAtKct2SKLI7Z7tCBVyW6QJsYwMHROvtCEDynbGSsBdqbwFcX+YrSlGnsg=', statusCode: 403, retryable: false, retryDelay: 30 }
2016-02-24T14:21:22.539Z baf9049b-db01-11e5-bc34-791df91353a9 Error getting object {"originalFilename":"c12eaadf3d3b46d9b5ded6c078534c11","versions":[{"Size":1024,"Crop":null,"Max":false,"Rotate":0}]} from bucket xmovo.originalimages.develop. Make sure they exist and your bucket is in the same region as this function.
2016-02-24T14:21:22.539Z baf9049b-db01-11e5-bc34-791df91353a9
{
"errorMessage": "Error getting object {\"originalFilename\":\"c12eaadf3d3b46d9b5ded6c078534c11\",\"versions\":[{\"Size\":1024,\"Crop\":null,\"Max\":false,\"Rotate\":0}]} from bucket xmovo.originalimages.develop. Make sure they exist and your bucket is in the same region as this function."
}
END RequestId: baf9049b-db01-11e5-bc34-791df91353a9
REPORT RequestId: baf9049b-db01-11e5-bc34-791df91353a9 Duration: 723.44 ms Billed Duration: 800 ms Memory Size: 128 MB Max Memory Used: 34 MB
我的lambda函数和我的S3存储桶位于相同的“ US Standart”和“ us-east-1”区域中
对于lambda函数,可以使用IAM权限,允许执行GetObject Action(由创建lambda函数的向导设置)
与所有的检查,我不知道为什么我仍然收到访问被拒绝错误
提前致谢
最佳答案
查看您的日志输出,我可以看到key
变量包含以下字符串:
{\"originalFilename\":\"c12eaadf3d3b46d9b5ded6c078534c11\",\"versions\":[{\"Size\":1024,\"Crop\":null,\"Max\":false,\"Rotate\":0}]}
我猜您打算让该变量包含字符串
"c12eaadf3d3b46d9b5ded6c078534c11"
。如果您无权访问或密钥不存在,S3将返回403错误响应。在两种情况下都返回“拒绝访问”是一项安全功能,可防止攻击者找出您的存储桶中实际存在的密钥。
我认为您需要更改此行:
decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
像这样:
decodeURIComponent(event.Records[0].s3.object.key.originalFilename.replace(/\+/g, ' '));
关于node.js - 当S3存储桶中的getObject时,对AWS Lambda函数的访问被拒绝,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35605622/