A developer is building a serverless photo-sharing application that allows users to upload JPEG images. The application uses an Amazon API Gateway REST API with an AWS Lambda proxy integration to process the uploads. During testing, the developer discovers that the image files processed by the Lambda function are corrupted because the binary payload is being treated as UTF-8 string data instead of binary data. Which configuration steps must the developer take to resolve this issue?
- AConfigure an Integration Request mapping template for image/jpeg in API Gateway to convert the binary stream to a JSON string before invoking the Lambda function.
- BSet the Integration Request's Content Handling setting to CONVERT_TO_TEXT in the API Gateway console, and read the raw binary stream from the Lambda function's context object.
- Add image/jpeg to the Binary Media Types of the API Gateway API, and update the Lambda function to base64-decode the body field from the input event when isBase64Encoded is true.Answer
- DConfigure a custom Lambda Authorizer to parse the incoming image payload and inject the decoded binary buffer into the request context.
Answer
Add the image/jpeg content type to the API Gateway API's Binary Media Types, and update the Lambda function code to base64-decode the event body when the isBase64Encoded flag is true.
For API Gateway to properly pass binary payloads (such as JPEG images) to a Lambda proxy integration, the API must be configured with the appropriate Binary Media Types. When the request Content-Type matches a binary media type, API Gateway base64-encodes the request body and sets the isBase64Encoded boolean flag to true in the event object. The Lambda function must check this flag and decode the body to get the raw binary content.
Step-by-Step Solution
Key Concept
Handling binary media payloads with API Gateway Lambda Proxy integration
Estimated Time:1m 30s