Posting image using form-data via API Gateway to Lambda function results in invalid file
Asked Answered
G

0

6

I've a Lambda function written in Go that supposed to allow image file upload—via an HTML form/JavaScript and an API Gateway integration—into an S3 bucket, all defined in a SAM template.

Here's an example of the Lambda function execution log:

enter image description here

Now the problem is that for any JPEG or PNG files I get corrupted results:

  • The file size of the resulting image (what I get once I parse the multipart media message) is larger than the original file size, for example: 8kB (original) to 12kB (S3 bucket).
  • When I compare the original PNG or JPEG files with the resulting ones in the S3 bucket with hexdump they almost look identical(that is to say: I doubt there's a base64 encoding issue involved).
  • Interestingly, for the text/plain media type this setup works.

I've tried to figure out where things break and at the current point in time my hunch is that there's something in the API Gateway configuration I am missing. FWIW, I tried this but no observable change in behavior:

$ aws apigateway update-integration-response \
                 --rest-api-id XXX --resource-id XXX \
                 --http-method POST --status-code 200 \
                 --patch-operations op='replace',path='/contentHandling',value='CONVERT_TO_BINARY'`
Goethite answered 29/11, 2018 at 11:24 Comment(3)
OK, I've found a workaround and maybe this is even a good practice: rather than directly uploading the image, I'm generating a presigned URL which is then used client-side to upload directly into the S3 bucket. See also the new handler function for implementation details.Goethite
it is definitely the recommended way to use pre signed links. You would end up having a lambda that creates the link but the upload is directly to S3 from the frontend. Unfortunately I didn't find a go example yet, but I have a php example :s. docs.aws.amazon.com/sdk-for-php/v3/developer-guide/… I face the same problem, but didn't come around to solving it yet. If you have a working go example I'd appreciate if you can share it.Wreathe
Thanks and yes, as mentioned above, the working Go example using pre-signed URLs is at github.com/mhausenblas/imgn/tree/master/functions/appGoethite

© 2022 - 2024 — McMap. All rights reserved.