gpt4 book ai didi

javascript - aws 将对象上传到 S3 存储桶并将数据详细信息传递给 lambda

转载 作者:太空宇宙 更新时间:2023-11-04 03:03:27 26 4
gpt4 key购买 nike

按照我的方式学习 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:参数中缺少必需的键“Source”* MissingRequiredParameter: 参数中缺少必需的键“Destination”* MissingRequiredParameter: params' 中缺少必需的键“Message”, 代码:'多重验证错误', 错误:

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

我在网上找不到太多信息......感谢任何帮助

更新好的,iv 让它正常工作而不会失败...如果我使用 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);

}
});

};

我收到了带有正文的电子邮件 - testData2null

因此,我尝试通过 s3 存储桶上传图像,但仍然收到正文为 testData2null 的电子邮件

有没有办法进一步调试这个问题,或者有人知道是谁说空的。我从未真正测试过另一篇文章中的代码,该代码将数据传递到我只是认为它会起作用的电子邮件。 还有谁知道从上传中获取数据吗?谢谢

最佳答案

您在 s3.getObject 的回调中声明 var eParams,但随后在回调之外运行 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);
}
});
}
});

关于javascript - aws 将对象上传到 S3 存储桶并将数据详细信息传递给 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47670028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com