如何在Lambda中将对象上传到S3? [英] How to upload an object into S3 in Lambda?
问题描述
似乎无法将对象上载到Lambda的S3中.本地一切正常.日志中没有错误,不会显示出问题所在...
Can't seem to upload an object into S3 in Lambda. Everything works fine locally. No errors in logs that would show what's going wrong...
以下代码:
console.log('Loading function');
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
exports.handler = function(event, context) {
//console.log(JSON.stringify(event, null, 2));
var s3 = new AWS.S3();
var param = {Bucket: 'flow-logs', Key: 'test-lambda-x', Body: 'me me me'};
console.log("s3");
s3.upload(param, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
console.log('done');
context.done();
};
成功运行且没有错误,但似乎未调用s3.upload中的回调.不会在存储桶中创建任何对象.
Runs successfully w/o error, but the callback in s3.upload doesn't seem to be called. No object in bucket is created.
通过授予完全访问权限以及在本地进行测试,验证的IAM角色权限不是问题.
Verified IAM role permissions weren't a problem by granting full access, as well as testing out locally.
输出
START RequestId: d4847fdb-160c-11e5-8a8c-b555b123e14d
2015-06-18T22:53:29.750Z d4847fdb-160c-11e5-8a8c-b555b123e14d s3
2015-06-18T22:53:30.271Z d4847fdb-160c-11e5-8a8c-b555b123e14d done
END RequestId: d4847fdb-160c-11e5-8a8c-b555b123e14d
推荐答案
我怀疑您在s3.upload()
有机会返回之前调用了context.done()
函数.如果将context.done()
移到上载响应代码块中,它应该可以工作.
I suspect you are calling the context.done()
function before s3.upload()
has a chance to return. If you move context.done()
into the upload response code block, it should work.
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
exports.handler = function(event, context) {
//console.log(JSON.stringify(event, null, 2));
var s3 = new AWS.S3();
var param = {Bucket: 'flow-logs', Key: 'test-lambda-x', Body: 'me me me'};
console.log("s3");
s3.upload(param, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
console.log('actually done!');
context.done();
});
console.log('done?');
//context.done();
};
这篇关于如何在Lambda中将对象上传到S3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!