Thursday 8 June 2017

why do const match always returns null

why do const match in the following code always returns null, or is there any better code to resize the image in s3 bucket with lambda function.. I'm striving with this code for more than 17 hours...

'use strict';
const AWS = require('aws-sdk');
const S3 = new AWS.S3({
accessKeyId: "xxxxxxxx",
secretAccessKey: "yyyy", 
region: "us-east-1", 
signatureVersion: 'v4',
 });
const Sharp = require('sharp');
 const BUCKET = imgbox";
 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))
}

output...

  TypeError: Cannot read property '1' of null



via V Abinaya

No comments:

Post a Comment