- Newest
- Most votes
- Most comments
Integration Request. Ensure that your Lambda function or backend service is correctly handling binary data. When using Lambda, configure it to process binary data properly.
Example for Lambda Proxy Integration:
import base64
import json
def lambda_handler(event, context):
try:
# Decode the base64 encoded body
body = base64.b64decode(event['body'])
# Save the image to S3 or process it as needed
s3_client.put_object(Bucket='your-upload-bucket', Key='uploaded_image.png', Body=body, ContentType='image/png')
return {
'statusCode': 200,
'body': json.dumps('Image uploaded successfully'),
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps(f'Error: {str(e)}'),
}
Ensure the API Gateway integration response is set to handle binary data correctly. ContentHandling property is set to CONVERT_TO_BINARY.
API Gateway Method Configuration
{
"httpMethod": "POST",
"resourceId": "your-resource-id",
"requestParameters": {},
"requestModels": {},
"requestValidatorId": null,
"operationName": "UploadImage",
"authorizerId": null,
"authorizationScopes": [],
"authorizationType": "NONE",
"apiKeyRequired": false,
"requestIntegration": {
"type": "AWS_PROXY",
"uri": "arn:aws:apigateway:region:lambda:path/2015-03-31/functions/arn:aws:lambda:region:account-id:function:your-function-name/invocations",
"httpMethod": "POST",
"credentials": null,
"requestParameters": {},
"requestTemplates": {},
"passthroughBehavior": "WHEN_NO_MATCH",
"contentHandling": "CONVERT_TO_BINARY",
"timeoutInMillis": 29000,
"cacheNamespace": "your-cache-namespace",
"cacheKeyParameters": []
},
"methodResponses": [
{
"statusCode": "200",
"responseParameters": {},
"responseModels": {}
}
]
}
Ensure that the image upload process correctly transfers the image data to the S3 bucket without corruption.
Verify Content-Type Metadata. S3 objects should have the correct Content-Type metadata set when uploading. If the Content-Type is incorrect, it may cause issues when the image is downloaded and viewed. For example, set Content-Type to image/jpeg for JPEG images, image/png for PNG images, etc.
Ensure that the API Gateway is correctly configured to handle binary data. Configure binary media types in API Gateway settings. https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html
If using CloudFront, ensure that the distribution settings correctly cache and serve the images.
Content-Type Metadata within the S3 buckets were matching the original uploaded image file type.
I tested a few things out and got the following results: Website Upload => corrupted image Postman Upload => corrupted image Direct Upload to S3 Bucket => good image
So my testing shows that once the image goes through the API Gateway is when it gets corrupted. A direct upload to S3 bucket and immediate downloading afterwards does not result in a corrupt image. It has to be something with the API Gateway.
I tried also setting the API Settings Binary Media Types to 'image/png' and */*. Both have not assisted in not corrupting image files of .png extension. Also took your advise on checking CloudFront and actually had to enabled the other HTTP requests (Like PUT) but did not cache the methods.
Relevant content
- AWS OFFICIALUpdated a year ago

I am not currently using Lambda but maybe that is the only solution, to integrate it.
please accept the answer if it was useful