Lambda函数中未运行S3 API的getObject回调内部的任何操作 [英] Nothing inside the S3 API's getObject callback is running in Lambda function
问题描述
我遇到了无法从S3读取文件的问题,甚至无法进入S3回调.我正在为lambda使用节点8.10,并且已经验证了一切都在运行,直到尝试进入getObject为止-下面的console.log
甚至都不会运行.这里有什么歪斜的地方吗?我已授予对lambda和S3的完全访问权限,所以我认为这不是问题.
I'm having a problem where I can't read my file from S3... or even get inside the S3 callback. I'm using node 8.10 for my lambda, and I've verified everything is running until I try to get inside of getObject -- the console.log
below won't even run. Does anything look askew here? I've granted full access to lambda and S3, so I don't think that's the issue.
const AWS = require('aws-sdk')
exports.handler = async (event, context, callback) => {
const s3options = {
accessKeyId: process.env.AWS_KEY,
secretAccessKey: process.env.AWS_SECRET,
apiVersion: '2006-03-01',
}
const params = {
Bucket: event.Records[0].s3.bucket.name,
Key: event.Records[0].s3.object.key,
}
const s3 = new AWS.S3(s3options)
s3.getObject(params, (err, data) => {
// callback(null, data.Body.toString('utf-8'))
console.log('I am here!')
})
}
推荐答案
如果尝试使用Node v8.x的 async/await 功能,则必须将代码包装到试试/ catch 块并使用promise(我的意思是没有必要包装功能代码,但是您仍然必须在应用程序内部实现try/catch块).
If you trying to use async/await feature of Node v8.x, then you have to wrap your code into try/catch block and use a promise (I mean it's not necessary to wrap your function code, but you still have to implement try/catch block inside your app).
注意:AWS-SDK已经实现,这意味着您没有来实现AWS-SDK方法或使用回调.只需将 .promise()作为尾部添加到您的方法中,然后将 await 关键字作为前缀添加到您尝试调用的方法中即可.
Note: AWS-SDK already promisified, means that you don't have to promisify AWS-SDK methods or use callbacks. Just simple append .promise() to your method as a tail, and add await keyword as a prefix to a method that is you trying to call.
示例:
之前:
s3.getObject(params, (err, data) => {
// callback(null, data.Body.toString('utf-8'))
之后:
try
{
const s3Response = await s3.getObject(params).promise();
// if succeed
// handle response here
}
catch (ex)
{
// if failed
// handle response here (obv: ex object)
// you can simply use logging
console.error(ex);
}
然后您的代码必须如下所示:
Then your code has to look like this:
// it's really cool to use ES6 syntax to import modules: import * as AWS from 'aws-sdk';
// btw, you don't have to import AWS-SDK inside the handler file
// const AWS = require('aws-sdk')
exports.handler = async (event) => {
const s3options =
{
accessKeyId: process.env.AWS_KEY,
secretAccessKey: process.env.AWS_SECRET,
apiVersion: '2006-03-01',
// do not forget include a region (e.g. { region: 'us-west-1' })
}
const params =
{
Bucket: event.Records[0].s3.bucket.name,
Key: event.Records[0].s3.object.key,
}
const s3 = new AWS.S3(s3options)
try
{
const s3Response = await s3.getObject(params).promise();
// if succeed
// handle response here
}
catch (ex)
{
// if failed
// handle response here (obv: ex object)
// you can simply use logging
console.error(ex);
}
}
这篇关于Lambda函数中未运行S3 API的getObject回调内部的任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!