Reputation: 1391
I'm currently in the process of moving a web-service behind an AWS API gateway, and I'm running into trouble with jpeg uploads.
The gateway at this point is a mere proxy. There's an ANY resource that just proxies everything to the service with the same path (right now it's mostly just intended to provide https support on an old url while the service moves domains). The content handling of the proxy is set to passthrough, so it shouldn't be re-encoding anything.
But when I now upload a jpg in a form-data request, the uploaded images are there, but cannot be opened. When trying to open them I get the error:
Not a JPEG file: starts with 0xef 0xbf
Now I now what you're thinking, you want to tell me that my jpg is actually a png. Believe me, it isn't. The pipeline delivering these images has worked reliably for years, I can open the jpgs before I upload them, and also I cannot open the files on the server if I rename them to .png. Somehow, the API gateway seems to corrupt the form-data passing through.
So I've done some Hexing with an image before and after passing through the gateway. The contents are identical, except for the first bytes in the file. Before the image passes through the gateway, the beginning of the file looks like this:
FF D8 FF E1 FF FE
After the upload, this is replaced with:
EF BF BD EF BF BD EF BF BD EF BF BD EF BF BD EF BF BD
Otherwise the files are identical. Why would the gateway be doing that, and how do I stop it? (according to the gateway logs, the content-type header of the incoming request is image/jpeg, so there shouldn't be issues with that either...)
Upvotes: 3
Views: 2335
Reputation: 238627
Based on the comments.
The issue was caused by not setting up Binary Media Types in API gateway:
In API Gateway, the API request and response have a text or binary payload. A text payload is a UTF-8-encoded JSON string. A binary payload is anything other than a text payload. The binary payload can be, for example, a JPEG file, a GZip file, or an XML file.
For AWS Lambda proxy integrations:
you must base64-encode your function's response. You must also configure the binaryMediaTypes for your API.
Upvotes: 4