本文介绍了无法创建使用AWS LAMBDA一个调整后的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个AWS lambda函数来调整是把我的S3存储桶任何图像。我已经创建了一个事件,每一个对象创建的S3存储桶的lambda函数来触发。我的处理程序$ C $下LAMBDA如下:

I have created an AWS Lambda function to resize any image that is put in my s3 bucket. I have created an event for every Object creation in s3 bucket for the Lambda function to trigger. My Handler code for Lambda is as follows:

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存储桶创建。我无法理解为什么我不能在相同的S3存储桶创建调整后的图像。有人可以帮我呢?

My Problem is that when I store an image in S3 the resized image is not getting created in my s3 Bucket. I am unable to understand why I am unable to create a resized image in the same s3 bucket. Can somebody help me with it?

推荐答案

它看起来像您所呼叫 context.done()的回调函数外请求()。这将结束lambda函数的任何图像大小调整完成之前。你应该只调用 context.done()当所有的处理已完成或失败。

It looks like you are calling context.done() outside the callback function for request(). This will end the Lambda function before any image resizing completes. You should only call context.done() when all processing has completed or failed.

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();
}

这篇关于无法创建使用AWS LAMBDA一个调整后的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 02:29