Last active
June 10, 2021 05:35
-
-
Save arturojain/2fff0680e2f4d6b62d0313ef0f9ac559 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var s3Zip = require('s3-zip') | |
var listObjects = function (opts) { | |
return new Promise (function (resolve, reject) { | |
var keys = [] | |
var getKeys = function (token) { | |
if (token) { | |
opts.ContinuationToken = token | |
} else { | |
delete opts.ContinuationToken | |
} | |
s3.listObjectsV2(opts, function (err, data) { | |
keys = keys.concat(data.Contents) | |
if (data.IsTruncated) { | |
getKeys(data.NextContinuationToken) | |
} else { | |
resolve(keys) | |
} | |
}) | |
} | |
getKeys() | |
}) | |
} | |
var zipFiles = function (request, response) { | |
listObjects({ | |
Bucket: bucket, | |
Prefix: folder | |
}) | |
.then(function (objects) { | |
objects = objects.filter(object => object.Size > 0) | |
objects = objects.map(object => object.Key) | |
response.set('content-type', 'application/zip') | |
response.set('content-disposition', 'attachment; filename="filename.zip"') | |
s3Zip | |
.archive({ | |
region: region, | |
bucket: bucket, | |
preserveFolderStructure: true | |
}, '', objects) // folder is empty due to the list of objects containing it | |
.pipe(response) | |
}) | |
} | |
app.get('/download', zipFiles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment