简体   繁体   English

在 Node.js 应用程序中从 AWS S3 下载文件

[英]Download files from AWS S3 in Node.js app

I have a code for uploading files to AWS S3 bucket:我有一个将文件上传到 AWS S3 存储桶的代码:

var upload = multer({
 storage: multerS3({
     s3: s3,
     bucket: 'mybucketname',
     key: function (req, file, cb) {
          cb(null, Date.now().toString())
     }
   }),
 fileFilter: myfilefiltergeshere...

})

I want to download the uploaded source.我想下载上传的源代码。 I don't know how could it be done, because I do not really know, how to identify the file on S3.我不知道怎么做,因为我真的不知道如何识别 S3 上的文件。 Is it the key field in the upload, or is it something else I have to specify?它是上传中的关键字段,还是我必须指定的其他内容?

For download you can如需下载,您可以

import AWS from 'aws-sdk'

AWS.config.update({
  accessKeyId: '....',
  secretAccessKey: '...',
  region: '...'
})

const s3 = new AWS.S3()

async function download (filename) {
  const { Body } = await s3.getObject({
    Key: filename,
    Bucket: 'mybucketname'
  }).promise()
  return Body
}

const dataFiles = await Promise.all(files.map(file => download(file)))

I have files in an array that's why i used files.map , but i guess you can look at the code for some guidance types, this might help you.我有一个数组中的文件,这就是我使用files.map的原因,但我想你可以查看一些指导类型的代码,这可能会对你有所帮助。 And for more you can read this .您可以阅读 更多内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM