Is it possible to upload to S3 directly from URL using POST?

前端 未结 3 1353
清酒与你
清酒与你 2020-12-03 04:24

I know there is a way to upload to S3 directly from the web browser using POST without the files going to your backend server. But is there a way to do it from URL instead o

相关标签:
3条回答
  • 2020-12-03 05:12

    It sounds like you want S3 itself to download the file from a remote server where you only pass the URL of the resource to S3.

    This is not currently supported by S3.

    It needs an API client to actually transfer the content of the object to S3.

    0 讨论(0)
  • 2020-12-03 05:14

    I thought I should share my code to achieve something similar. I was working on the backend but possibly could do something similar in frontend though be mindful about AWS credentials likely to be exposed.

    For my purposes, I wanted to download a file from the external URL and then ultimately get back the URL form S3 of the uploaded file instead.

    I also used axios in order to get the uploadable format and file-type to get the proper type of the file but that is not the requirement.

    Below is the snippet of my code:

    async function uploadAttachmentToS3(type, buffer) {
      var params = {
       //file name you can get from URL or in any other way, you could then pass it as parameter to the function for example if necessary
        Key : 'yourfolder/directory/filename', 
        Body : buffer,
        Bucket : BUCKET_NAME,
        ContentType : type,
        ACL: 'public-read' //becomes a public URL
      }
      //notice use of the upload function, not the putObject function
      return s3.upload(params).promise().then((response) => {
        return response.Location
      }, (err) => {
        return {type: 'error', err: err}
      })
    }
    
    async function downloadAttachment(url) {
      return axios.get(url, {
        responseType: 'arraybuffer'
      })
      .then(response => {
        const buffer = Buffer.from(response.data, 'base64');
        return (async () => {
          let type = (await FileType.fromBuffer(buffer)).mime
          return uploadAttachmentToS3(type, buffer)
        })();
      })
      .catch(err => {
        return {type: 'error', err: err}
      });  
    }
    
    let myS3Url = await downloadAttachment(url)
    

    I hope it helps people who still struggle with similar issues. Good luck!

    0 讨论(0)
  • 2020-12-03 05:15

    I found this article with some details. You will probably have to modify your buckets' security settings in some fashion to allow this type of interaction.

    http://aws.amazon.com/articles/1434

    There will be some security issues on the client as well since you never want your keys publicly accessible

    0 讨论(0)
提交回复
热议问题