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?
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.
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.
The API gateway doesn't introduce a single point of failure any more than a load balancer does.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With