aws将对象上传到S3存储桶并将数据细节传递给lambda

时间:2017-12-06 08:39:18

标签: javascript node.js amazon-web-services amazon-s3 aws-lambda

通过AWS的教程工作......所以我创建了一个S3存储桶,当一个文件被放入其中时调用我的lambda'testHelloWorld'发送一封电子邮件......这一切都正常(见下文)

 'use strict';

console.log('Loading function');

var aws = require('aws-sdk');
var ses = new aws.SES({
   region: 'us-west-2'
});

exports.handler = function(event, context) {
    console.log("Incoming: ", event);
   // var output = querystring.parse(event);

    var eParams = {
        Destination: {
            ToAddresses: ["johnb@hotmail.com"]
        },
        Message: {
            Body: {
                Text: {
                    Data: "Hey! What is up?"
                }
            },
            Subject: {
                Data: "Email Subject!!!"
            }
        },
        Source: "johnb@hotmail.com"
    };

    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("===EMAIL SENT===");
            console.log(data);


            console.log("EMAIL CODE END");
            console.log('EMAIL: ', email);
            context.succeed(event);

        }
    });

};

但是我想扩展电子邮件以包含上传到存储桶的文件的数据。我找到了How to trigger my Lambda Function once the file is uploaded to s3 bucket,它给出了一个应该捕获数据的node.js代码片段。我试图将它导入我现有的lambda

 'use strict';

console.log('Loading function');

var aws = require('aws-sdk');
var ses = new aws.SES({
   region: 'us-west-2'
});
var s3 = new aws.S3({ apiVersion: '2006-03-01', accessKeyId: process.env.ACCESS_KEY, secretAccessKey: process.env.SECRET_KEY, region: process.env.LAMBDA_REGION });

exports.handler = function(event, context, exit){
    console.log("Incoming: ", event);
   // var output = querystring.parse(event);


 // Get the object from the event and show its content type
   // const bucket = event.Records[0].s3.bucket.name;
   // const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
    const params = {
       Bucket: 'bucketName',
       Key: 'keyName',
       Source : 'SourceName',
       Destination : 'DestinationName',
       Message : 'MessageName'
    };


     s3.getObject(function(err, data){
         if (err) {
           console.log('ERROR ' + err);
          // exit(err);
         } else {
           // the data has the content of the uploaded file
           var eParams = {
        Destination: {
            ToAddresses: ["johnboy@hotmail.com"]
        },
        Message: {
            Body: {
                Text: {
                    Data: data
                }
            },
            Subject: {
                Data: "Email Subject!!!"
            }
        },
        Source: "johnboy@hotmail.com"
    };
         }
     });     


    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("===EMAIL SENT===");
            console.log(data);


            console.log("EMAIL CODE END");
            console.log('EMAIL: ', email);
            context.succeed(event);

        }
    });

};

但这在参数

上失败了
message: 'There were 3 validation errors:

* MissingRequiredParameter:在params中缺少必需的键\'Source \' * MissingRequiredParameter:在params中缺少必需的键\'Destination \' * MissingRequiredParameter:在params'中缺少必需的键''Message \',   代码:'MultipleValidationErrors',   错误:

这些来源,目的地和消息都列在参数中,它们是否格式不正确并且没有将它们捡起来?

我无法在线找到任何帮助

更新 好的,我的工作没有失败...如果我使用lambda中的测试函数,使用以下代码...

   'use strict';

console.log('Loading function');

var aws = require('aws-sdk');
var ses = new aws.SES({
   region: 'us-west-2'
});

var s3 = new aws.S3({ apiVersion: '2006-03-01', accessKeyId: process.env.ACCESS_KEY, secretAccessKey: process.env.SECRET_KEY, region: process.env.LAMBDA_REGION });


exports.handler = function(event, context) {
    console.log("Incoming: ", event);
   // var output = querystring.parse(event);

   var testData = null;

   // Get the object from the event and show its content type
   // const bucket = event.Records[0].s3.bucket.name;
   // const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
    const params = {
       Bucket: 'bucket',
       Key: 'key',
    };

     s3.getObject(params, function(err, data){
         if (err) {
           console.log('ERROR ' + err);
           exit(err);
         } else {
           testData = data;
         }
     }); 

    var eParams = {
        Destination: {
            ToAddresses: ["jim@him.com"]
        },
        Message: {
            Body: {
                Text: { Data: 'testData2' + testData}
            },
            Subject: {
                Data: "Email Subject!!!"
            }
        },
        Source: "jim@him.com"
    };

    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("===EMAIL SENT===");
            console.log(data);


            console.log("EMAIL CODE END");
            console.log('EMAIL: ', email);
            context.succeed(event);

        }
    });

};

我收到了body-testData2null

的电子邮件

所以我尝试通过s3存储桶上传图像,我仍然收到带有testData2null主体的电子邮件

无论如何都要进行进一步的调试,或者是否有人知道它是否为null。我从来没有真正测试过其他帖子中的代码,该帖子将数据传递给我认为可行的电子邮件。 Does anyone else know who to obtain the data from the upload please? thanks

2 个答案:

答案 0 :(得分:2)

您在var eParams的回调中声明s3.getObject,但随后在回调之外运行ses.sendMail。我想这就是原因!

如果您想从电子邮件中的对象发送数据,还需要将ses.sendEmail移至s3.getObject的回调内。

试试这个:

s3.getObject(function(err, objectData) {
    if (err) {
        console.log('Could not fetch object data: ', err);
    } else {
        console.log('Data was successfully fetched from object');
        var eParams = {
            Destination: {
                ToAddresses: ["johnboy@hotmail.com"]
            },
            Message: {
                Body: {
                    Text: {
                        Data: objectData
                    }
                },
                Subject: {
                    Data: "Email Subject!!!"
                }
            },
            Source: "johnboy@hotmail.com"
        };

        console.log('===SENDING EMAIL===');

        var email = ses.sendEmail(eParams, function(err, emailResult) {
            if (err) console.log('Error while sending email', err);
            else {
                console.log("===EMAIL SENT===");
                console.log(objectData);
                console.log("EMAIL CODE END");
                console.log('EMAIL: ', emailResult);
                context.succeed(event);
            }
        });
    }
});

答案 1 :(得分:2)

您需要了解Nodejs的工作原理。它是基于事件的,取决于回调和承诺。你应该做 -

 s3.getObject(params, function(err, data){
      //This is your callback for s3 API call. DO stuff here
     if (err) {
       console.log('ERROR ' + err);
       exit(err);
     } else {
       testData = data;
        // Got your data. Send the mail here
     }
 }); 

我在上面的代码中添加了我的评论。由于Nodejs是单线程的,它将进行S3 api调用并继续。当它发送邮件时s3 api调用未完成,因此数据为空。最好在这里使用promises。

无论如何,请阅读nodejs中的回调和承诺以及它是如何工作的。但希望这能解决你的逻辑错误。