简体   繁体   English

通过API网关AWS Service Proxy上传到S3存储桶

[英]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. 我正在尝试使用Postman发送带有Content-Type:image / png的PUT请求,并且正文是png文件的二进制上载。

I've also tried using curl: 我也尝试过使用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. 当我尝试使用.txt文件(content-type:text / plain)时,它似乎可以工作。

Any ideas? 有任何想法吗?

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. 在阅读了很多并与AWS技术支持人员聊天之后,问题似乎是您无法通过API网关进行二进制上传,因为任何自动传递的内容都会通过UTF-8编码。

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 我可以想到一些解决方法,我的解决方案是在上传之前将文件作为base64并在它们到达桶时触发lambda来解码它们

This is a old post, but I got a solution. 这是一个老帖子,但我得到了一个解决方案。 AWS now support binary upload through APIGateway READ . AWS现在通过APIGateway READ支持二进制上传。

In general, go to your API settings, and add a Binary Media type. 通常,转到您的API设置,然后添加二进制媒体类型。

After that, you can handle the file in base64 之后,您可以在base64中处理该文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM