Home > Net >  Can I upload form-data files to S3 via AWS API-Gateway?
Can I upload form-data files to S3 via AWS API-Gateway?

Time:06-24

In AWS environment I want to upload a file to S3 via API gateway. I can't use lambda, because the 6MB payload limit. (The API gateway's 10MB limit is fine.) I could manage to upload a file with POST request and binary body. 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?

CodePudding user response:

You should use aws-sdk to create the API end point and get the url:

  • API Code (aws-sdk API Doc):

    • for upload, reference s3.getSignedUrl('putObject')
    • Don't use S3.putObject for upload document, it is a difference concept
    • 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):

    • Select your bucket
    • Permissions Tab > Cross-origin resource sharing (CORS) > Edit
        [
          {
            "AllowedHeaders": [
              "*",
              "Authorization"
            ],
            "AllowedMethods": [
              "POST",
              "GET",
              "PUT",
              "DELETE",
              "HEAD"
            ],
            "AllowedOrigins": [
              "*"
            ],
            "ExposeHeaders": [],
            "MaxAgeSeconds": 3000
          }  
        ]
      
    • Confirm your AllowedMethods existing PUT method
  • Upload Document(Postman case)

    • Copy and paste the link which get from 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: '*/*'}
      • Example: {Accept: 'image/png' }
    • Send with PUT method
  • now you may check the s3 bucket

other helpful reference: https://medium.com/@aidan.hallett/securing-aws-s3-uploads-using-presigned-urls-aa821c13ae8d

  • Related