S3 API 的 getObject 回调中的任何内容都不是 Lambda 函数中的 运行
Nothing inside the S3 API's getObject callback is running in Lambda function
我遇到了无法从 S3 读取文件的问题...甚至无法进入 S3 回调。我正在为我的 lambda 使用节点 8.10,并且我已经验证了一切都是 运行ning,直到我尝试进入 getObject 内部——下面的 console.log
甚至不会 运行。这里有什么东西看起来歪了吗?我已经授予对 lambda 和 S3 的完全访问权限,所以我认为这不是问题所在。
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 功能,那么您必须将您的代码包装到 try/catch 块并使用承诺(我的意思是没有必要包装你的函数代码,但你仍然必须在你的应用程序中实现 try/catch 块)。
注意:AWS-SDK 已经承诺,意味着您没有承诺AWS-SDK 方法或使用回调。只需将 .promise() 作为尾部附加到您的方法中,然后将 await 关键字作为前缀添加到您尝试调用的方法中.
示例:
之前:
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);
}
那么您的代码必须如下所示:
// 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);
}
}
我遇到了无法从 S3 读取文件的问题...甚至无法进入 S3 回调。我正在为我的 lambda 使用节点 8.10,并且我已经验证了一切都是 运行ning,直到我尝试进入 getObject 内部——下面的 console.log
甚至不会 运行。这里有什么东西看起来歪了吗?我已经授予对 lambda 和 S3 的完全访问权限,所以我认为这不是问题所在。
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 功能,那么您必须将您的代码包装到 try/catch 块并使用承诺(我的意思是没有必要包装你的函数代码,但你仍然必须在你的应用程序中实现 try/catch 块)。
注意:AWS-SDK 已经承诺,意味着您没有承诺AWS-SDK 方法或使用回调。只需将 .promise() 作为尾部附加到您的方法中,然后将 await 关键字作为前缀添加到您尝试调用的方法中.
示例:
之前:
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);
}
那么您的代码必须如下所示:
// 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);
}
}