8

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?

2 Answers 2

2

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')
Sign up to request clarification or add additional context in comments.

Comments

-1

Sounds like you're using AWS integration type of API Gateway instead of LAMBDA integration and in that case API Gateway would expect entire message to be base64 encoded, not just the body. For your use case you probably should use LAMBDA integration and return json with statusCode, body, headers, and Content-Type as you currently do.

1 Comment

Integration type is already set as LAMBDA if this is what you are referring to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.