How to download multiple files from S3 and download as a zip file

We are now working on a dashboard which can browse the files on S3 bucket and need a feature to download a zip file containing multiple specific image files on the S3 bucket. Is that possible to do that?
Currently I can download individual image via Download a file from S3 in query but cannot find a way to achieve the requirement mentioned above... Any suggestion for this? Thanks!

2 Likes

I'm looking for a way to do this too! Hopefully there's a solution for this

2 Likes

Hi there! We don't have a native solution for this, but you should be able to add an external library to solve for this.

In this example, I'm using this external library: https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js

Then, I have a JS query that runs the following, where query3 and query5 are S3 read queries:

var zip = new JSZip();

var img = zip.folder("images");

img.file(query3.data.Key, query3.data.Body, {base64: true});

img.file(query5.data.Key, query5.data.Body, {base64: true});

zip.generateAsync({type:"base64"}).then(function (base64) {

utils.downloadFile({base64Binary: base64}, "images", "zip")

}, function (err) {

utils.showNotification({title: "Error", description: err, notificationType: "Error"})

});

2 Likes

@Tess
OK so I am at the point where I can download the folder but nothing is in it!

All the keys are there: Each Key is in the following format: parentfolder/clientname/type/filename.pdf
There are 2 keys total.
I had to iterate through the result when I read the bucket location because the Contents is an array:
Screen Shot 2022-06-09 at 3.12.58 PM

var sdocs = zip.folder("Payment-");
for(i=0; i<listAllFilesForZip.data.Contents.length; i++){

sdocs.file(readEachFileToAddToZip.fileKey, readEachFileToAddToZip.data.Body, {base64: true})

console.log('logging ' + i + ' ' + listAllFilesForZip.data.Contents[i].Key)
}

Screen Shot 2022-06-09 at 4.08.12 PM

Any thoughts?

This has been resolved by splitting the process into a few queries:
First: List all files in bucket
Then read each file to gather all files by running the following JS Query

const promises = listAllFilesForZip.data.Contents.map((row) => {
  return readEachFileToAddToZip.trigger({
    additionalScope: {
      Key: row.Key
    },
  });
});

return Promise.all(promises);

then in another JS Query, create the zip file:

var zip = new JSZip(); 
let folder = "Payment-"

var sdocs = zip.folder(folder);

getListedFiles.data.map(x=>{
  sdocs.file(x.Key, x.Body,{base64: true})
})

This was resolved by @Tess - Thank you!

2 Likes