【发布时间】:2015-07-07 23:41:38
【问题描述】:
我已经搜索过这里(即How do you structure sequential AWS service calls within lambda given all the calls are asynchronous?)和其他地方的帖子,但似乎找不到一点点信息可以帮助我解决这个烦人的问题。 当您有一个遍历循环的 Lambda 函数,并且在该循环中调用 s3.putObject() 时,它会在尝试正确处理 context.succeed()/context.fail 时遇到短路问题() 或旧的 context.done(null, 'msg') 关闭 Lambda 进程的方式。
I.E.迭代需要使用要上传的当前对象调用 s3.putObject(),但仍将成功上传的文件输出到 cloudwatch 或 SQS/SNS。但是,我将这种类型的闭包放入函数中的所有尝试都会遇到随机结果,有时会获取文件名,有时只获取一些文件名,等等。
最好的方法是什么?我尝试使用 Q 和 async,但老实说,我仍在学习所有这些东西..
以下是我正在尝试做的一个粗略示例:
function output(s3Object){
s3.putObject(s3Object, function(err, data){
if (err) {
console.log('There was an issue with outputting the object.', err);
} else {
// how do you properly close this if you have x number of incoming calls??
// context.done(null, 'success');
}
// and later in the code where it actually calls the output function
// and NOTE: it should output all of the file names that the invocation uploads!
for (var a = 0; a < myRecords.length; a++){
output(myRecords[a]);
}
但是,正如我之前所说,到目前为止,我所做的任何尝试都会得到不同的结果。
Successfully output object: myBucket/prefix/part_000000123432345.dat
Successfully output object: myBucket/prefix/part_000000123432346.dat
但是函数输出的另一个测试:
Successfully output object: myBucket/prefix/part_000000123432346.dat
啊。
【问题讨论】:
标签: node.js asynchronous amazon-web-services amazon-s3 aws-lambda