如何使用Node.js向Amazon S3发出请求以下载文件?

huangapple go评论105阅读模式
英文:

How to make request to amazon s3 using node js for downloading a file?

问题

I made a request to amazon s3 using node js for downloading a file. In the response i receive the Buffer, here i have 2 problems:

  1. If i send the buffer to frontend from node js like res.send(awsreq.Body.Buffer) and console log it once in node js and once in frontend, the buffer from frontend will look different than in node js.

Node.js Buffer: <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 df a4 d2 6c 5a 01 00 00 20 05 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c 20 ... 11906 more bytes>
Vue.js frontend buffer: PK\u0003\u0004\u0014\u0000\u0006\u0000\b\u0000\u0 Show more(36,8kb)

  1. How can i directly download the file from a request made from frontend? First how to receive that buffer correctly and how to convert it in that way frontend will download the file automatically? If possible how to do all of that in node js and when frontend receive the response to start automatically the download?
英文:

I made a request to amazon s3 using node js for downloading a file. In the response i receive the Buffer, here i have 2 problems:

  1. If i send the buffer to frontend from node js like res.send(awsreq.Body.Buffer) and console log it once in node js and once in frontend, the buffer from frontend will look different than in node js.
    Node js Buffer: <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 df a4 d2 6c 5a 01 00 00 20 05 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c 20 ... 11906 more bytes>
    Vue js frontend buffer: PK\u0003\u0004\u0014\u0000\u0006\u0000\b\u0000\u0 Show more(36,8kb)
  2. How can i directly download the file from a request made from frontend? First how to receive that buffer corecly and how to convert it in that way frontend will download the file automatically? If possible how to do all of that in node js and when frontend receive the response to start automatically the download?

答案1

得分: 0

要从Amazon S3下载文件,请按照以下步骤操作:

  1. 使用以下命令安装AWS SDK:

    npm install aws-sdk
    
  2. 检查下面的代码:

var AWS = require("aws-sdk");

const s3 = new AWS.S3({
    endpoint: "ENDPOINT",
    accessKeyId: "YOUR_ACCESS_KEY_ID",
    secretAccessKey: "YOUR_SECRET_ACCESS_KEY",
    region: "us-east-1",
    signatureVersion: "v4"
})

const downloadParams = {
  Bucket: "BUCKET_NAME",
  Key: "KEY_NAME/FILE_NAME/FILE_PATH"
}

// 下载文件
s3.getObject(downloadParams, function (error, fileData) {
  if (!error) {
    console.log(fileData, "file");
  } else {
    console.log(error, "ERROR");
  }
})

获取更多信息,您可以查看AWS官方文档,使用以下链接:

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/getting-started-nodejs.html

最后,您可以在请求标头中使用Content-Disposition,或者您可以使用预签名URL。例如:

const getPreSignedUrlForDownload = async () => {
  try {
    const params = {
      Bucket: "BUCKET_NAME",
      Key: "FILE_KEY",
      Expires: 60 * 1,
      ResponseContentDisposition: 'attachment; filename="' + FILE_NAME + '"'
    }
    return new Promise((resolve, reject) => {
      s3.getSignedUrl("getObject", params, (err, url) => {
        err ? reject(err) : resolve(url);
      })
    })
  } catch (error) {
    throw new Error(error);
  }
}

getPreSignedUrlForDownload()

希望这些信息对您有帮助。

英文:

To Download a file from amazon S3 follow these steps

  1. Install aws sdk using this command npm install aws-sdk
  2. Check below code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var AWS = require(&quot;aws-sdk&quot;)

const s3 = new AWS.S3({
    endpoint: &quot;ENDPOINT&quot;,
    accessKeyId: &quot;YOUR_ACCESS_KEY_ID&quot;,
    secretAccessKey: &quot;YOUR_SECRET_ACCESS_KEY&quot;,
    region: &quot;us-east-1&quot;,
    signatureVersion: &quot;v4&quot;
  })


const downloadParams = {
  Bucket: &quot;BUCKET_NAME&quot;,
  Key: &quot;KEY_NAME/FILE_NAME/FILE_PATH&quot;
}

// Download the file
s3.getObject(downloadParams, function (error, fileData) {
  if (!error) {
    console.log(fileData, &quot;file&quot;)
  } else {
    
    console.log(error, &quot; ERROR &quot;)
  }
})

<!-- end snippet -->

For more information you can check AWS official documentation using this link:

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/getting-started-nodejs.html

At last you can use
Content-Disposition in the request header

Or you can use pre Signed url

Example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const getpreSignedUrlForDownload = async () =&gt; {
  try {
  
    const parms = {
      Bucket: &quot;BUCKET_NAME&quot;,
      Key: &quot;FILE_KEY&quot;,
      Expires: 60 * 1,
      ResponseContentDisposition: &#39;attachment; filename&quot;&#39; + &quot;FILE_NAME + &#39;&quot;&#39;
    }
    return new Promise((resolve, reject) =&gt; {
      s3.getSignedUrl(&quot;getObject&quot;, parms, (err, url) =&gt; {
        err ? reject(err) : resolve(url)
      })
    })
  } catch (error) {
    throw new Error(error)
  }
}

getpreSignedUrlForDownload()

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月8日 23:29:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定