简体   繁体   中英

Download files from AWS S3 in Node.js app

I have a code for uploading files to AWS S3 bucket:

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. 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. And for more you can read this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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