简体   繁体   English

如何使用 node.js 向 amazon s3 请求下载文件?

[英]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.我使用 node.js 向 amazon s3 发出了下载文件的请求。 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 \b \u0 Show more(36,8kb)如果我将缓冲区从 node.js 发送到前端,例如 res.send(awsreq.Body.Buffer) 并且控制台在 node.js 中记录一次,然后在前端记录一次,那么来自前端的缓冲区看起来将与 node.js 中的不同。Node.js 缓冲区:<缓冲区 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 6d 76 7c 8d 5d . 11906 更多字节> Vue.js 前端缓冲区:PK \b \u0 显示更多(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?首先如何接收该缓冲区 corecly 以及如何以这种方式转换它,前端将自动下载文件? If possible how to do all of that in node.js and when frontend receive the response to start automatically the download?如果可能的话,如何在 node.js 中完成所有这些操作,以及当前端收到响应以自动开始下载时?

To Download a file from amazon S3 follow these steps要从amazon S3下载文件,请执行以下步骤

  1. Install aws sdk using this command npm install aws-sdk使用此命令安装 aws sdk npm install aws-sdk
  2. Check below code检查下面的代码

 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" } // Download the file s3.getObject(downloadParams, function (error, fileData) { if (.error) { console,log(fileData. "file") } else { console,log(error, " ERROR ") } })

For more information you can check AWS official documentation using this link:有关更多信息,您可以使用此链接查看 AWS 官方文档:

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/getting-started-nodejs.html 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最后你可以在请求中使用Content-Disposition header

Or you can use pre Signed url或者您可以使用预签名 url

Example:例子:

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

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

相关问题 我们如何使用 node.js 将 m3u8 文件和相应的 ts 文件上传到 amazon s3 存储桶 - How can we upload m3u8 file and corresponding ts file to amazon s3 bucket using node.js 使用 Node.js Vue.js 在前端显示来自 Amazon S3 私有存储桶的图像 - Show images from Amazon S3 private bucket in frontend using Node js Vue js 如何使用 React js 将图像上传到 Amazon S3 存储桶? - How to upload image to an Amazon S3 bucket using React js? 如何使用 Node.js 的 AWS SDK 将 Amazon S3 中的所有对象从一个前缀复制/移动到另一个前缀 - How to copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js 如何使用 cURL 从 Amazon S3 存储桶中删除文件 - How to delete a file from amazon S3 bucket using cURL 使用 node.js 在 S3 上提取 7z 文件 - extract 7z file on S3 using node.js 如何使用 node.js 将 object 从 s3 复制到 s3 - How to copy the object from s3 to s3 using node.js 如何使用 postdata preSigned Url 调用 Amazon S3 存储桶以使用空手道上传文件 - How to call Amazon S3 bucket using postdata preSigned Url to upload a file using Karate 如何将文件上传到 S3 并使用 boto3 将其公开? - How to upload a file to S3 and make it public using boto3? 如何使用他们的 s3cmd 工具将文件放在 Amazon S3 存储桶中? - How do I put a file on an Amazon S3 bucket using their s3cmd tools?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM