2

I am using node's sharp library to do image processing,
I am using AWS Lambda with node 10x version to resize the image, as recommended in the sharp library's documentation.

I am downloading the image file from AWS S3,
To AWS Lambda's /tmp directory,
And reading the file in /tmp directory as an input for the sharp library code.

I am getting the following error -

[Error: Input file contains unsupported image format]

Following is my sample code -
1. handler.js

let service = require('./service');

exports.handler = async (event) => {
    try {
        let requestBody = {};

        console.log('Event =>\n',JSON.stringify(event));

        requestBody.bucketName = 'bucketName';
        requestBody.s3FilePath = 'images/test.jpg';

        requestBody.fileName = requestBody.s3FilePath.substring(requestBody.s3FilePath.lastIndexOf('/')+1,requestBody.s3FilePath.length);
        requestBody.originalFilePath = '/tmp/'+requestBody.fileName;
        requestBody.thumbnailFileName = 'thumb_'+requestBody.fileName;
        requestBody.thumbnailFilePath = '/tmp/'+requestBody.thumbnailFileName;

        console.log('requestBody =>\n',requestBody);

        let serviceResponse = await service.processImage(requestBody);
        if(serviceResponse){
            console.log('Image => ',requestBody.fileName,' processed');
            return true;
        }
        else{
            console.log('Could not process => ',requestBody.fileName);
            return false;
        }
    }
    catch(error){
        console.log('Error =>\n', error);
    }
};

2. service.js -

let sharp = require('/opt/layer/node_modules/sharp');
let AWS = require('/opt/layer/node_modules/aws-sdk');
let fs = require('fs');

module.exports.processImage = async (requestBody) => {

    let isFileDownload = await downloadImage(requestBody);
    if(!isFileDownload){
        throw('Unable to get Object from S3');
    }

    let isThumbnailCreated = await createThumbnail(requestBody);
    if(!isThumbnailCreated){
        throw('Unable to create thumbnail');
    }

    return true;
};

let downloadImage = async (requestBody) => {
    let client = new AWS.S3({
        region: 'us-east-1'
    });

    var params = {
        Bucket: requestBody.bucketName, 
        Key: requestBody.s3FilePath
    };

    var file = await fs.createWriteStream(requestBody.originalFilePath);
    await client.getObject(params).createReadStream().pipe(file);

    return true;
};

let createThumbnail = async (requestBody) => {
    await sharp(requestBody.originalFilePath)
    .resize({
        width: '320',
        height: '320',
        fit: sharp.fit.outside
    })
    .sharpen()
    .toFile(requestBody.thumbnailFileName)
    .then(info => { 
        console.log('Image processing success response =>\n',info);
     })
    .catch(error => {
        console.log('Image processing failure response =>\n',error);
    });

    return true;
};

This is my working test snippet -

const sharp = require('sharp');

let test = async () => {

    await sharp('pathToFile/test.jpg')
    .resize({
        width: 320,
        height: 320,
        fit: sharp.fit.outside
    })
    .sharpen()
    .toFile('sharp_320x320.jpg')
    .then(info => { 
        console.log(info);
     })
    .catch(err => {
        console.log(err);
    });
}

test();

Reference - https://sharp.pixelplumbing.com/en/stable/api-resize/#examples

Dev1ce
  • 5,390
  • 17
  • 90
  • 150

0 Answers0