Get multiple images from aws S3 with node js

Viewed 440

I am trying to get all the images from S3 bucket folder. I have used s3.listObjectsV2 to get the key and using s3.geObjects to get images. my code below

router.route('/getImages/').post((req, res) => {
   var result = [];
   var params = { Bucket: 'bucketName', Prefix: 'parent_folder/child_folder'};
   s3.listObjectsV2(params, (err, data) => {
       if (err === null) { 
          data.Contents.map(item=>{
              var getParam = { Bucket: 'bucketName', Key: item.Key};
              s3.getObject(getParam, function (err, resp) {
                 if (err === null) {
                     result += resp.Body
                     console.log(result) // consoles the result
                 } else {
                     res.send(err)
                 }
             });
         })
         res.send(result) // sends empty array
         console.log(result) // consoles empty array
     }else{
         return res.send({ "error": err });
     }
})

also, I have to display the image from the response to the front end React. The images in the S3 are not public and I cannot use the URL, I have to FS read the resp.body Please help

0 Answers
Related