Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a binary response with AWS Lambda with API Gateway in Node.js? [duplicate]

I'm trying to set up a Lambda and API Gateway that will do a s3.getObject() and output the binary image as a response. Eventually I'd like to pull an image from s3 and resize on the fly instead of saving them back to s3, however I can't seem to get even a simple image to output.

My simple lambda looks like this:

'use strict';

const http = require('http');    

exports.handler = (event, context, callback) => {
    http.get('http://i.stack.imgur.com/PIFN0.jpg', function(res) {
        var body = '';
        res.on('data', function(chunk) {
            // Agregates chunks
            body += chunk;
        });
        res.on('end', function() {
            callback(null, body);
        });
    });    
};

I've set the API Gateway Binary Support to allow 'image/jpeg' and I've tried setting the Content Type in the Method Response and Integration Response.

Method Response: enter image description here

Integration Response: enter image description here

like image 728
tkiethanom Avatar asked Jan 05 '17 23:01

tkiethanom


People also ask

How do I add binary media type API gateway?

Under the selected API in the primary navigation panel, choose Settings. In the Settings pane, choose Add Binary Media Type in the Binary Media Types section. Type a required media type, for example, image/png , in the input text field. If needed, repeat this step to add more media types.

Can Lambda run binaries?

The Lambda execution environment is based on a specific Amazon Linux AMI and kernel version. Any native binaries that are used in a Lambda deployment package must be compiled in this environment, and only 64-bit binaries are supported.


1 Answers

I found my answer here: AWS Gateway API base64Decode produces garbled binary?

It requires a CLI command to change a setting that isn't exposed in the AWS Console when you select Lambda Function on the Create Method screen.

like image 196
tkiethanom Avatar answered Oct 06 '22 01:10

tkiethanom