1

I'm making a dynamic image resize tool using Lambda, API gateway and Cloudfront.

The URL is like this.

https://xxx.cloudfront.net/restaurant-101o.png&w=200

But unfortunately, the API returns

{"errorMessage":"Error getting object restaurant-101o.png&w=200 from bucket (bucketname). Make sure they exist and your bucket is in the same region as this function."}

In short, lambda can't separate 2 parameters, which is 'filename' and 'w'. Also, API gateway is set like this.

enter image description here

What should I do? Thanks in advance.

'use strict';

console.log('Loading function');

const im = require('imagemagick');
const aws = require('aws-sdk');

const s3 = new aws.S3({ apiVersion: '2006-03-01' });


exports.handler = (event, context, callback) => {
    const bucket = 'xxxx-contents';
    const filename = decodeURIComponent(event.filename);
    const width = decodeURIComponent(event.w);
    const params = {
        Bucket: bucket,
        Key: 'images/' +filename.split('-')[0]+'/'+filename.split('-')[1],
    };
    s3.getObject(params, (err, data) => {
        if (err) {
            console.log(err);
            var message = "Error getting object " + filename + " from bucket " + bucket +
                ". Make sure they exist and your bucket is in the same region as this function.";
            console.log(message);
            context.fail(message);
        } else {
            var contentType = data.ContentType;
            var extension = contentType.split('/').pop();

            im.resize({
                srcData: data.Body,
                format: extension,
                width: width
            }, function(err, stdout, stderr) {
                if(err) {
                    context.fail(err);
                    return;
                }
                callback(null, new Buffer(stdout, 'binary').toString('base64'))
            });
        }
    });
};
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Noriaki Takamizawa
  • 887
  • 1
  • 10
  • 25

1 Answers1

0

Did you check the "Use Lambda Proxy Integration" when you created the API gateway entry?

You can find more information here: https://stackoverflow.com/a/46114185/501217

Kim Burgaard
  • 3,508
  • 18
  • 11