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

前端 未结 3 1377
清酒与你
清酒与你 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: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!

提交回复
热议问题