aheckmann / gm

GraphicsMagick for node
http://aheckmann.github.com/gm/
6.95k stars 615 forks source link

works locally, but on Lambda getting "params.Body is required" error #840

Open mkantautas opened 2 years ago

mkantautas commented 2 years ago

Nodejs 12.x

Using this lamda function to convert from heic to jpg and store to my s3 bucket. works locally, but on Lambda getting "params.Body is required" error

"use strict";
const gm = require('gm').subClass({imageMagick: true});

const root = process.env['LAMBDA_TASK_ROOT'];
const path = process.env['PATH'];
const libPath = process.env['PATH'];
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
// change the Lambda Runtime to use pre-built binary
process.env['PATH'] = `${root}/bin:${path}`;
process.env['LD_LIBRARY_PATH'] = `${root}/lib:${libPath}`;

// convert HEIC to JPEG
module.exports.handler = async (event, context, callback) => {
    let from = event.source_img_path;
    let to = event.destination_img_path
    let thumbnail = event.thumbnail
    // main
    let image = gm(from)
        .noProfile()

    if (thumbnail) {
        image = image.resize("128^", "128^")
            .crop(128, 128)
    }

    image.toBuffer('jpg', function (err, buffer) {
        let params = {Bucket: process.env.BUCKET_NAME, Key: to, Body: buffer};
        new Promise(resolve => {
            s3.upload(params, function (err, result) {
                if (err) {
                    console.log(err)
                    throw new Error('Could not upload photo');
                } else {
                    console.log(result)
                    resolve(result);
                }
            })
        })
    })
};

Does anyone know how to resolve this? Or the issue is that you can't use 'toBuffer' on Lambda? Thanks in advance.

1001Josias commented 2 years ago

Add an imagemagick layer in lambda?