简体   繁体   English

我可以通过 AWS API-Gateway 将表单数据文件上传到 S3 吗?

[英]Can I upload form-data files to S3 via AWS API-Gateway?

In AWS environment I want to upload a file to S3 via API gateway.在 AWS 环境中,我想通过 API 网关将文件上传到 S3。 I can't use lambda, because the 6MB payload limit.我不能使用 lambda,因为 6MB 有效负载限制。 (The API gateway's 10MB limit is fine.) I could manage to upload a file with POST request and binary body. (API 网关的 10MB 限制很好。)我可以设法上传一个带有 POST 请求和二进制正文的文件。 My question is how can I upload a picture which is wrapped in a multipart/form-data and can I set a file size and format limitation without using lambda functions?我的问题是如何上传包含在 multipart/form-data 中的图片,并且可以在不使用 lambda 函数的情况下设置文件大小和格式限制吗?

You should use aws-sdk to create the API end point and get the url:您应该使用 aws-sdk 创建 API 端点并获取 url:

  • API Code(aws-sdk API Doc) : API 代码(aws-sdk API 文档)

    • for upload, reference s3.getSignedUrl('putObject')对于上传,参考s3.getSignedUrl('putObject')
    • Don't use S3.putObject for upload document, it is a difference concept不要使用S3.putObject上传文件,这是一个不同的概念
    • Example:例子:
       return new Promise((resolve, reject) => { let url = this.S3.getSignedUrl("getObject", { Bucket: bucket, Key: key, Expires: expires_second }) return resolve(url)
  • S3 Bucket Config (Reference Link) : S3 存储桶配置(参考链接)

    • Select your bucket选择您的存储桶
    • Permissions Tab > Cross-origin resource sharing (CORS) > Edit权限选项卡 > 跨域资源共享 (CORS) > 编辑
       [ { "AllowedHeaders": [ "*", "Authorization" ], "AllowedMethods": [ "POST", "GET", "PUT", "DELETE", "HEAD" ], "AllowedOrigins": [ "*" ], "ExposeHeaders": [], "MaxAgeSeconds": 3000 } ]
    • Confirm your AllowedMethods existing PUT method确认您的AllowedMethods现有PUT方法
  • Upload Document(Postman case)上传文件(邮递员案例)

    • Copy and paste the link which get from s3.getSignedUrl('putObject')复制并粘贴从s3.getSignedUrl('putObject')获取的链接
    • Body tab > binary > select you document正文选项卡 > 二进制 > 选择您的文档
    • Headers > uncheck 'Content-Type', 'User-Agent'标头 > 取消选中“内容类型”、“用户代理”
    • [Optional] if u want to accept selected file type, edit { Accept: '*/*'} [可选] 如果您想接受选定的文件类型,请编辑{ Accept: '*/*'}
      • Example: {Accept: 'image/png' }示例: {Accept: 'image/png' }
    • Send with PUT method使用PUT方法发送
  • now you may check the s3 bucket现在您可以检查 s3 存储桶

other helpful reference : https://medium.com/@aidan.hallett/securing-aws-s3-uploads-using-presigned-urls-aa821c13ae8d其他有用的参考https ://medium.com/@aidan.hallett/securing-aws-s3-uploads-using-presigned-urls-aa821c13ae8d

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

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