Thursday 8 June 2017

match and originalkey to resize Image in lambda function

I'm working with lambda function to resize the upload image in my s3 bucket, I'm encountering few problems with the following code, Can anyone explain what is "const match" and "originalKey" I tried several combination . none helped me.

 'use strict';
 const AWS = require('aws-sdk');
 const S3 = new AWS.S3({
 accessKeyId: "xxxxxxxxxxxx",
 secretAccessKey: "yyyyyyyyyyy", 
region: "us-east-1", 
signatureVersion: 'v4',
});

const Sharp = require('sharp');
const BUCKET = "patientimg"; 
const URL = "https://s3.ap-south-1.amazonaws.com";
exports.handler = function(event, context, callback) {
const key = event.Records[0].s3.object.key;
const match = key.match(/(\d+)x(\d+)\/(.*)/);
const width = parseInt(match[1], 10);
const height = parseInt(match[2], 10);
const originalKey = match[3];

S3.getObject({Bucket: BUCKET, Key: originalKey}).promise()
.then(data => Sharp(data.Body)
 .resize(width, height)
  .toFormat('png')
    .toBuffer()
 )
 .then(buffer => S3.putObject({
   Body: buffer,
    Bucket: BUCKET,
     ContentType: "image/png",
       Key: key,
        }).promise()
    )
.then(() => callback(null, {
   statusCode: '301',
    headers: {'location': "${URL}/${key}"},
      body: "",
  })
)
 .catch(err => callback(err))
}  

Thanks in advance.....



via V Abinaya

No comments:

Post a Comment