[英]How do I access an S3 object from a Lambda function?
I am attempting to access a text file stored in an AWS S3 bucket.我正在尝试访问存储在 AWS S3 存储桶中的文本文件。 At present, it contains only the word "Test".目前,它只包含“测试”一词。
At first, I thought I was having problems with fs.readfile, but now I've discovered that the problem is more fundamental.起初,我以为我的 fs.readfile 有问题,但现在我发现问题更根本。 I cannot access the file at all.我根本无法访问该文件。 AWS from Node.js does not appear to be able to see the file at all.来自 Node.js 的 AWS 似乎根本无法看到该文件。
I am using the following Lambda function:我正在使用以下 Lambda function:
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
exports.handler = async (event, context, callback) => {
// Get the object from the event and show its content type
const bucket = event.Records[0].s3.bucket.name;
const key = event.Records[0].s3.object.key; // decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
console.log('Bucket = ' + bucket);
console.log('key = ' + key);
var params = {Bucket: bucket, Key: key};
console.log('Checking file existence');
console.log(params);
console.log('Calling s3.getObject');
s3.getObject(params, function(err, data) {
console.log('S3.getObject called');
console.log('err = ' + err);
if (err) {
console.log(err, err.stack); // an error occurred
callback(err);
} else {
console.log(data); // successful response
callback(null, null);
}
console.log('Leaving s3.getObject');
});
};
The testbed function contains the following code:测试台 function 包含以下代码:
{
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "us-east-1",
"eventTime": "1970-01-01T00:00:00.000Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"responseElements": {
"x-amz-request-id": "EXAMPLE123456789",
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "wgtiplists",
"ownerIdentity": {
"principalId": "EXAMPLE"
},
"arn": "arn:aws:s3:::wgtiplists"
},
"object": {
"key": "tiplist.txt",
"size": 1024,
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901"
}
}
}
]
}
And the results look like this (I've trimmed off the timestamp and Request ID texts):结果看起来像这样(我已经剪掉了时间戳和请求 ID 文本):
INFO Bucket = s3://wgtiplists
INFO key = tiplist.txt
INFO Checking file existence
INFO { Bucket: 's3://wgtiplists', Key: 'tiplist.txt' }
INFO Calling s3.getObject
From this, I conclude that the S3 function is not being called, although I might be mistaken.由此,我得出结论,没有调用 S3 function,尽管我可能弄错了。
What am I doing wrong?我究竟做错了什么?
I believe you need parenthesis here:我相信你在这里需要括号:
const s3 = new aws.S3();
You should not be adding s3://
to the bucket name, just do this:您不应该将s3://
添加到存储桶名称中,只需执行以下操作:
const bucket = event.Records[0].s3.bucket.name;
Look for the Cloudwatch logs.查找 Cloudwatch 日志。 You might get some insight from there.你可能会从那里得到一些见解。 Probably, as @Marck B mentioned, should be given bucket name is wrong可能正如@Marck B 提到的那样,应该给出存储桶名称是错误的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.