无法使用AWS Lambda

时间:2015-09-21 14:32:45

标签: amazon-web-services amazon-s3 aws-lambda

我创建了一个AWS Lambda函数来调整放在我的s3存储桶中的任何图像的大小。我已经为s3存储桶中的每个Object创建了一个事件,以便触发Lambda函数。我的Lambda处理程序代码如下:

var async = require('async'),
    gm = require('gm').subClass({
    imageMagick: true
}) // Enable ImageMagick integration.
, request = require('request').defaults({
    encoding: null
});

var knox = require('knox');
var client = knox.createClient({
key: 'myKey',
secret: 'mySecretKey',
bucket: 'MyBucketName'
});


//hardcoded Value to get the Image url
var s3value = '.s3.amazonaws.com/';
var https = 'https://'

exports.handler = function (event, context) {
var srcBucket = event.Records[0].s3.bucket.name;
var srcKey = event.Records[0].s3.object.key;
var imageUrl = https + srcBucket + s3value + srcKey //My Http Url for image stored in s3 bucket.
request(imageUrl, function (err, res, res1) {
    if (err) {
        console.log(err);
    } else {
        gm(res1).resize(120)
            .toBuffer('jpg', function (err, buffer) {
                if (err) {
                    console.log(err);
                } else {
                    var ImageName = "/" + imageUrl.substr(0, imageUrl.indexOf(".")) + "-1x" + imageUrl.substr(imageUrl.indexOf("."), imageUrl.length); //Renaming the present image
                    client.putBuffer(buffer, ImageName, function (err, response1) {
                        if (err) {
                            console.log(err);
                        } else {
                            console.log(response1.socket._httpMessage.url);
                        }
                    });
                }
            });
    }
});
context.done();
}

我的问题是,当我在S3中存储图像时,调整大小的图像不会在我的s3 Bucket中创建。我无法理解为什么我无法在同一个s3存储桶中创建调整大小的图像。有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

看起来你在context.done()的回调函数之外调用了request()。这将在任何图像大小调整完成之前结束Lambda函数。您应该只在所有处理完成或失败时调用context.done()

exports.handler = function (event, context) {
    var srcBucket = event.Records[0].s3.bucket.name;
    var srcKey = event.Records[0].s3.object.key;
    var imageUrl = https + srcBucket + s3value + srcKey //My Http Url for image stored in s3 bucket.
    request(imageUrl, function (err, res, res1) {
        if (err) {
            console.log(err);
            context.fail(err);
        } else {
            gm(res1).resize(120)
                .toBuffer('jpg', function (err, buffer) {
                    if (err) {
                        console.log(err);
                        context.fail(err);
                    } else {
                        var ImageName = "/" + imageUrl.substr(0, imageUrl.indexOf(".")) + "-1x" + imageUrl.substr(imageUrl.indexOf("."), imageUrl.length); //Renaming the present image
                        client.putBuffer(buffer, ImageName, function (err, response1) {
                            if (err) {
                                console.log(err);
                                context.fail(err);
                            } else {
                                console.log(response1.socket._httpMessage.url);
                                context.succeed("It worked");
                            }
                        });
                    }
                });
        }
    });
    // Don't call context.done() here, the callback hasn't run yet
    //context.done();
}