Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API Gateway unable to decode base64

I am attempting to use a node based lambda function to return jpeg images from s3, using API Gateway.

My Lambda function reads as:

s3.getObject(params).promise().then((result) => { 
    let resp = {
                statusCode: 200,
                headers: {
                    'Content-Type': 'image/jpeg'
                },
                body: result.Body.toString('base64'),
                isBase64Encoded: true
    };          
    callback(null, resp);
});

I have also modified the integration response in API gateway to "Convert to binary (if needed)". When I try testing this function I receive the error "Execution failed due to configuration error: Unable to base64 decode the body.".

Is there a step I am missing to allow me to retrieve base64 encoded files?

like image 611
Rabona Avatar asked Apr 10 '17 14:04

Rabona


People also ask

Can base64 encoding be decoded?

Encoding files is not encryption and should never be used to secure sensitive data on disk. Rather it is a useful way of transferring or storing large data in the form of a string. While it may obfuscate that actual data from should surfers, anyone who has access to base64 encoded data can easily decode it.

What is the problem that base64 encoding method design to solve?

The base64 encoding scheme is typically used when there is a need to encode binary data that needs to be stored and transferred through media designed to deal with textual data. This is to ensure that the data remains intact without modification during shipping.

Can API gateway fail?

The API gateway doesn't introduce a single point of failure any more than a load balancer does.

How do I pass AWS API gateway request body?

In the Mapping Templates area, choose an option for Request body passthrough to configure how the method request body of an unmapped content type will be passed through the integration request without transformation to the Lambda function, HTTP proxy, or AWS service proxy.


1 Answers

I'm not sure about it, but have you tried to use this instead of the toString called directly on your object?

Buffer.from(result.Body).toString('base64')
like image 191
balsick Avatar answered Oct 19 '22 05:10

balsick