Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload to S3 bucket through API Gateway AWS Service Proxy

As in the title, I can't seem to get it to work, i'm following the high level guide detailed here but any images uploaded seem to be blank.

What i've set up:

/images/{object} - PUT
 > Integration Request
   AWS Region: ap-southeast-2
   AWS Service: S3
   AWS Subdomain [bucket name here]
   HTTP method: PUT
   Path override: /{object}
   Execution Role [I have one set up]
 > URL Path Paramaters
   object -> method.request.path.object

I'm trying to use Postman to send a PUT request with Content-Type: image/png and the body is a binary upload of a png file.

I've also tried using curl:

curl -X PUT -H "Authorization: Bearer [token]" -H "Content-Type: image/gif" --upload-file ~/Pictures/bart.gif https://[api-url]/dev/images/cool.gif

It creates the file on the server and the size seems to be double what ever was uploaded, when viewed I just get "image has an error".

When I try with .txt files (content-type: text/plain) it seems to work though.

Any ideas?

like image 275
csilk Avatar asked Oct 19 '22 08:10

csilk


1 Answers

After reading alot and chatting to AWS technical support, the problem seems to be that you can't do binary uploads through API Gateway as anything that passes through automatically goes through a UTF-8 encode.

There are a few workarounds for this I can think of, my solution will be to base64 the files before upload and trigger a lambda when they hit the bucket to decode them

like image 107
csilk Avatar answered Oct 21 '22 05:10

csilk