【发布时间】:2016-08-14 21:08:06
【问题描述】:
我的 aws-lambda 函数中有一个异步函数,效果很好。
这样做:
抓取图片(“下载”功能),
裁剪并将其调整为缩略图(“转换”功能),
将该缩略图上传到新存储桶(“上传”功能),
使用缩略图的 url 更新场地对象(这是一个 Parse 对象)('updateVenue' 函数)
最后,它创建一个新的场景对象(这也是一个解析对象)('saveScene' 函数)。
**我省略了指定场地对象和场景对象的代码以使其更简单,因为我认为这不是问题。
我的问题是在 updateVenue 函数被记录为成功完成后,下一个日志是:Process exited before completed request.也就是 saveScene 函数永远不会被调用。
即使我颠倒了 updateVenue 和 saveScene 函数的顺序,在第一个 Parse 函数 - saveScene 完成后,该过程也会退出。因此,我认为错误在于我调用这些的方式。
我也在使用 context.succeed() ,也许这与它有关?
// Download the image from S3, transform, and upload to a different S3 bucket.
async.waterfall([
function download(next) {
// Download the image from S3 into a buffer.
s3.getObject({
Bucket: srcBucket,
Key: srcKey
},
next);
},
function transform(response, next) {
gm(response.Body).size(function(err, size) {
// Infer the scaling factor to avoid stretching the image unnaturally.
WIDTH = size.width;
HEIGHT = size.height;
if (WIDTH > HEIGHT) {
var side = HEIGHT;
}
else{
var side = WIDTH;
}
var scalingFactor = Math.min(
MAX_WIDTH / side,
MAX_HEIGHT / side
);
var width = scalingFactor * side;
var height = scalingFactor * side;
// Transform the image buffer in memory.
this.gravity("Center").crop(side, side).resize(width, height)
.toBuffer(imageType, function(err, buffer) {
if (err) {
next(err);
console.log(err);
} else {
next(null, response.ContentType, buffer);
}
});
});
},
function upload(contentType, data, next) {
// Stream the transformed image to a different S3 bucket.
s3.putObject({
Bucket: dstBucket,
Key: dstKey,
Body: data,
ContentType: contentType
},
next);
},
function updateVenue(next) {
venueObj.save(null, {
success: function(response){
console.log('Updated Venue thumbnail succesfully: ', response);
context.succeed();
next
},
error: function(response, error){
console.log('Failed to update Venue thumbnail, with error code: ' + error.description);
context.fail();
next
}
}); // end of venueObj.save
},
function saveScene(next) {
sceneObj.save(null, {
success: function(response){
console.log('Saved sceneObj succesfully: ', response);
context.succeed();
next
},
error: function(response, error){
console.log('Failed to create new sceneObj, with error code: ' + error.description);
context.fail();
next
}
}); // end of sceneObj.save
}
], function (err) {
if (err) {
console.error(
'Unable to resize ' + srcBucket + '/' + srcKey +
' and upload to ' + dstBucket + '/' + dstKey +
' due to an error: ' + err
);
} else {
console.log(
'Successfully resized ' + srcBucket + '/' + srcKey +
' and uploaded to ' + dstBucket + '/' + dstKey
);
}
callback(null, "message");
}
);
【问题讨论】:
标签: javascript node.js amazon-web-services parse-platform aws-lambda