MonkeyBonkey
MonkeyBonkey

Reputation: 47901

How do I respond with a raw buffer instead of toString in Amazon API Gateway - Lambda

Here is my code thus far - I'd like to just return the image buffer as raw data rather than having it toString to an array.

I set the content-type to image/jpeg in the integration response on a http 200 response, but it's a broken image because I think it's a toString of the buffer rather than the raw data.

exports.handler = function(event, context) {

    var srcKey = event.key || 'e_1.png';
    var max_size = event.size || 100;


    // Download the image from S3
    s3.getObject({
        Bucket: srcBucket,
        Key: srcKey
    }, function (err, response) {
        if (err)
            return context.fail('unable to download image ' + err);
        var original = gm(response.Body);
        original.size(function (err, size) {
            if (err)
                return context.fail('unable to download image ' + err);

            resize_photo(size, max_size, original, function (err, photo) {
                //res.setHeader('Content-Type', 'image/jpeg');
                context.succeed(photo);
            });
        });
    });

};

Upvotes: 2

Views: 849

Answers (1)

adamkonrad
adamkonrad

Reputation: 7122

This doesn't seem like something Lambda with API Gateway was designed for. Binary output may not be supported given the state of it's pipeline. Try doing something else instead - store the image back to the S3 and send back a HTTP Redirect to new S3 URI. Let the client handle it instead of trying to make the API Gateway pipeline handle binary responses.

Upvotes: 1

Related Questions