API gateway corrupting images bound for S3

0

i am uploading jpg files from my android app , to an S3 bucket via an API gateway. I have logging turned on at the gateway. It appears that the file is being converted although I have set the binary type jpeg to pass through. How can I solve this?

GatewayLog

  • Did you specify correct content type header to the request?

asked a month ago195 views
2 Answers
1
Accepted Answer

When you upload binary files like JPG through API Gateway, you need to ensure that the files are not modified during the transmission. API Gateway provides options to handle binary data properly. Here are a few steps you can take to solve this issue:

  1. Enable Binary Support in API Gateway:

    • Go to the API Gateway console and select your API.
    • Under the "Settings" section, click on "Binary Media Types".
    • Add the image/jpeg media type to the list of binary types.
    • Save the changes.
  2. Set the Content-Type Header Correctly:

    • In your Android app, when making the request to upload the JPG file, set the Content-Type header to image/jpeg.

    • Example in Java:

      MediaType mediaType = MediaType.parse("image/jpeg");
      RequestBody requestBody = RequestBody.create(mediaType, fileBytes);
  3. Use the Appropriate HTTP Method:

    • API Gateway treats binary payloads differently based on the HTTP method used.
    • For file uploads, it's recommended to use the PUT or POST method.
  4. Enable Payload Compression:

    • In the API Gateway console, go to your API's "Settings".
    • Under "Binary Media Types", ensure that "Payload Compression" is disabled.
    • This setting ensures that the binary data remains uncompressed during transmission.
  5. Check API Gateway Mapping Templates:

    • API Gateway uses mapping templates to transform request and response data.
    • Ensure that the mapping templates for your API method are not modifying the binary data.
    • You can find the mapping templates under the "Integration Request" and "Integration Response" sections of your API method.
  6. Check Lambda Function (if using):

    • If your API Gateway is integrated with a Lambda function, ensure that the Lambda function is not modifying the binary data.
    • You may need to handle the binary data differently in your Lambda function code.

By following these steps, you should be able to upload JPG files from your Android app to the S3 bucket without any modifications to the binary data.

profile pictureAWS
answered a month ago
0

thanks for your responses. I checked all these things and everything was looking good. I added multipart/form-data to the binary media types and also added Access-Control-Allow-Origin to the Methd Reponse Header for "PUT" and mage/jpeg to Method Response Body. It was error free after this

answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions