2

This is my first lambda function on AWS.

I just want to convert a JPG on S3 to a PNG. So far I've got this:

'use strict';

let aws = require('aws-sdk');
let im = require('imagemagick');
let s3 = new aws.S3();

exports.handler = (event, context, callback) => {
    const bucket = event.Records[0].s3.bucket.name;
    const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
    const s3params = {
        Bucket: bucket,
        Key: key
    };
    s3.getObject(s3params, function(err,s3obj) {
        //s3obj has the image in .Body...
    });
}

I've been trying to find out how to use imagemagick to convert this JPG to a PNG. The convert function seems to require files not buffers. I can't work out how to do it and there doesn't seem like an examples I can find.

Help!

Simon Sawyer
  • 317
  • 1
  • 2
  • 11
  • Refer this article http://jice.lavocat.name/blog/2015/image-conversion-using-amazon-lambda-and-s3-in-node.js/ – Piyush Patil Jun 04 '16 at 12:20
  • Thanks, but I saw that article, however, firstly that does resizing not simple conversion and secondly it doesn't work. If I use that it says that it can't find the module "gm" - I had to change it to require "imagemagick" not "gm". – Simon Sawyer Jun 04 '16 at 16:08
  • "gm" is most likely in the GraphicsMagick package, not in ImageMagick. – Glenn Randers-Pehrson Jun 04 '16 at 21:06
  • gm doesn't exist in lambda node.js but imagemagick does. What's the difference? Can I use imagemagick or do I need graphicsmagick and if so how do i install that? – Simon Sawyer Jun 06 '16 at 08:42

1 Answers1

0

Here's how I've pulled in graphicsmagick on lambda: var gm = require('gm').subClass({ imageMagick: true });

Note that imagemagick some functionality (EPHEMRAL, URL, HTTPS, MVG and MSL) has been disabled in the imagemagick pre-installed module by AWS Lambda team due to vulnerabilities discovered in ImageMagick library in early May (see https://imagetragick.com) and explained further here: AWS Lambda not working along with the gm module. Normally you can use gm to pull an image from the web and then you'd save that stream as your png but you may be running into the above issues on that

Community
  • 1
  • 1