5

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?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
csilk
  • 2,732
  • 3
  • 23
  • 40
  • Does the object actually created in S3 also have the correct Content-Type, as shown in the console? Does the ETag in S3 match the md5 hash of the original file? – Michael - sqlbot Jun 07 '16 at 11:19
  • Content type seems to be the same, an md5 does not match the ETag... interesting – csilk Jun 07 '16 at 23:26
  • Also the local file md5 does not match the md5 of the uploaded file (if I download it again), just tried .txt files and they seem to work :| – csilk Jun 08 '16 at 00:11
  • Updated original question with this extra info :) – csilk Jun 08 '16 at 00:31

2 Answers2

2

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

csilk
  • 2,732
  • 3
  • 23
  • 40
  • 1
    I'm not sure if this applies to your situation, but at the bottom of this document: http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-s3.html ...there's a note: "To upload or download an image, you need to set content handling to CONVERT_TO_BINARY." – Matt Aug 15 '17 at 19:59
1

This is a old post, but I got a solution. AWS now support binary upload through APIGateway READ.

In general, go to your API settings, and add a Binary Media type.

After that, you can handle the file in base64

hook38
  • 3,899
  • 4
  • 32
  • 52