我已经在这里搜索过帖子(即,鉴于所有调用都是异步的,你如何在 lambda 中构建顺序 AWS 服务调用?)和其他地方,似乎找不到一点点信息可以帮助我过去这个烦人的问题。当您有一个遍历循环的 Lambda 函数,并且在该循环中调用 s3.putObject() 时,它会在尝试正确处理 context.succeed()/context.fail 时遇到短路问题() 或旧的 context.done(null, 'msg') 关闭 Lambda 进程的方式。
IE 迭代需要调用 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
啊。