Thursday, 8 June 2017

Resizing the image in S3 bucket from lambda trigger using nodejs

I newbie to nodejs and aws, I found a program in internet to resize the images in s3 bucket using lambda trigger, but it doesn't work the way as it should,

Program as follows

// dependencies

var async = require('async');
var AWS = require('aws-sdk');
var gm = require('gm').subClass({ imageMagick: true }); 
var util = require('util'); 
var path = require('path');

//constrains
var WEB_WIDTH_MAX  = 150;
var WEB_HEIGHT_MAX = 150;
var imageResponse;

// get reference to S3 client 
var s3 = new AWS.S3();
AWS.config.loadFromPath("./config.json");
AWS.config.update({
accessKeyId: "xxxxxxx",
secretAccessKey: "yyyyyyyy", 
"region": "zzz" 
 });

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 imageName=path.basename(srcKey);
var dstBucket = srcBucket;
var imageResponse ;

// Infer the image type.
var typeMatch = srcKey.match(/\.([^.]*)$/);
if (!typeMatch) {
    callback("Could not determine the image type.");
    return;
}
var imageType = typeMatch[1];
if (imageType.toUpperCase() != "jpg".toUpperCase() && imageType.toUpperCase() != "png".toUpperCase() && imageType.toUpperCase() != "jpeg".toUpperCase()) {
    callback('Unsupported image type: ${imageType}');
     return;
}
console.log("****************before async******************");
// 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: patientimg,
                Key: srcKey
            },
            next);
        },
  function transformWebMax(response, next) {
        gm(response.Body)
          .resize(WEB_WIDTH_MAX, WEB_HEIGHT_MAX, '^')
          .gravity('Center')
          .crop(WEB_WIDTH_MAX, WEB_HEIGHT_MAX)
          .toBuffer('jpg',function (err, buffer) {
           if (err) return handle(err);
                next(null, response, buffer);
        });
    },
  function uploadWebMax(response, buffer, next) {
        // Stream the transformed image to a different S3 bucket.
        var dstKeyResized    = "resized/"+imageName;
        s3.putObject({
                Bucket: patientimg,
                Key: dstKeyResized,
                Body: buffer,
                ContentType: response.ContentType
            }, function(err, data) {
            if (err) {
                console.log(err, err.stack);
            }else{
                console.log('uploaded to web-max Successfully !!');
                next(null, response, buffer);
            }
        });
    }
  ], function (err) {
        if (err) {
            console.log(
                'Unable to resize image'
            );
        } else {
            console.log(
                'Successfully resized image'
            );
        }
callback(null, "message");
    }
);
};

While clicking save and test option in aws lambda console.. I have got the following error

"errorMessage": "RequestId: 2bb1b55b-4c3a-11e7-92ef-c896c05731a2 Process exited before completing request"

Procedure I followed ....

1.downloaded all dependency locally and zip it with a name "aws.zip", My zip file contains

node_modules

config.json

index.js

and then uploaded.



via anna poorani

No comments:

Post a Comment