Nothing inside the S3 API's getObject callback is running in Lambda function

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 03:23:48

问题


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!')
    })

}

回答1:


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).

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.

Example:

Before:

s3.getObject(params, (err, data) => {
        // callback(null, data.Body.toString('utf-8'))

After:

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);
    }
}


来源:https://stackoverflow.com/questions/52825290/nothing-inside-the-s3-apis-getobject-callback-is-running-in-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!