AWS Lambda函数在完成请求之前退出

时间:2016-09-16 19:00:24

标签: node.js amazon-web-services amazon-s3 aws-lambda

我试图在这里关注S3和Lambda的AWS指南

http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html

我正处于手动测试阶段,但我得到" errorMessage":"在完成请求之前退出流程"当我尝试进行测试时。

在日志中,失败点与async.waterfall代码有关。

当我运行Lambda函数时,它会获取我的示例图像,调整其大小并将其放入新的S3存储桶中。然后看起来async试图找到一个未定义的" nextTask"跑步。我之前从未使用异步,因此我不确定如何解决此问题。

cloudwatch日志文件为我提供了这个

2016-09-16T18:36:44.011Z    836d0280-7c3c-11e6-933a-9b5b3a5e8dd8    TypeError: undefined is not a function
at /var/task/ResizeImages.js:98:13
at /var/task/node_modules/async/dist/async.js:486:20
at nextTask (/var/task/node_modules/async/dist/async.js:5008:33)
at Response.<anonymous> (/var/task/node_modules/async/dist/async.js:5015:17)
at Response.<anonymous> (/var/task/node_modules/async/dist/async.js:339:31)
at Response.<anonymous> (/var/task/node_modules/async/dist/async.js:847:20)
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:355:18)
at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:615:14)

要安装async(和imageMagik),我只使用了命令

npm install async gm

在我的node_modules文件夹中。

我使用的示例节点脚本在这里,第98行被注释。

// dependencies
var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm')
            .subClass({ imageMagick: true }); // Enable ImageMagick integration.
var util = require('util');

// constants
var MAX_WIDTH  = 100;
var MAX_HEIGHT = 100;

// get reference to S3 client 
var s3 = new AWS.S3();

exports.handler = function(event, context, callback) {
    // Read options from the event.
    console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));
    var srcBucket = event.Records[0].s3.bucket.name;
    // Object key may have spaces or unicode non-ASCII characters.
    var srcKey    = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));  
    var dstBucket = srcBucket + "-resized";
    var dstKey    = "thumb-" + srcKey;

    // Sanity check: validate that source and destination are different buckets.
    if (srcBucket == dstBucket) {
        callback("Source and destination buckets are the same.");
        return;
    }

    // Infer the image type.
    var typeMatch = srcKey.match(/\.([^.]*)$/);
    if (!typeMatch) {
        callback("Could not determine the image type.");
        return;
    }
    var imageType = typeMatch[1];
    if (imageType != "jpg" && imageType != "png") {
        callback('Unsupported image type: ${imageType}');
        return;
    }

    // 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.
                var scalingFactor = Math.min(
                    MAX_WIDTH / size.width,
                    MAX_HEIGHT / size.height
                );
                var width  = scalingFactor * size.width;
                var height = scalingFactor * size.height;

                // Transform the image buffer in memory.
                this.resize(width, height)
                    .toBuffer(imageType, function(err, buffer) {
                        if (err) {
                            next(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 (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");
        } // ------- LINE 98 -----------
    );
};

1 个答案:

答案 0 :(得分:2)

发布可见性答案。

运行时设置为nodejs而不是node4.3