我既不是Node.js也不是AWS Lambda专家,所以我可能会做错事。 我编写了下面的代码,使用AWS CLI从命令行执行时效果很好,但是在lambda函数中不会调用它。
您能告诉我要查找或调试的内容以了解原因吗? 感谢。
var aws = require('aws-sdk');
console.log('Loading function');
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
console.log('value1 =', event.key1);
var params = {
Bucket: 'nicobucket1',
Body: 'hellolll',
Key: 'mykey',
};
var s3 = new aws.S3();
console.log('Created S3 object, invoking upload...' + JSON.stringify(s3));
s3.upload(params, function(err, data){
console.log('...in function upload...');
if(err){
console.log(err);
context.fail('Could not create object in bucket...');
}
else console.log ('Uploaded data, check the console...');
});
context.succeed('Created object in S3 bucket!');
};
我在日志中看到除了s3.upload函数中的所有console.log消息。当然不会创建对象。 从命令行一切都很好。
答案 0 :(得分:0)
context.succeed();
最有可能在s3.upload()
有机会完成之前执行。尝试将context.succeed()'
行移动到s3.upload函数中。我还注意到在mykey'之后你的params中有一个额外的逗号。我已经尝试了以下修改后的代码版本并且成功了。
var aws = require('aws-sdk');
console.log('Loading function');
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
console.log('value1 =', event.key1);
var params = {
Bucket: 'nicobucket1-unique',
Body: 'hellolll',
Key: 'mykey'
};
var s3 = new aws.S3();
console.log('Created S3 object, invoking upload...' + JSON.stringify(s3));
s3.upload(params, function(err, data){
console.log('...in function upload...');
if(err){
console.log(err);
context.fail('Could not create object in bucket...');
}
else
{
console.log ('Uploaded data, check the console...');
context.succeed('Created object in S3 bucket!');
}
});
};

希望这有助于某人。