Created
April 17, 2022 14:37
-
-
Save pacarvalho/b2fdd6a3df05037021b252d6f70d003b to your computer and use it in GitHub Desktop.
Utility Function to Download Files from S3
This file contains 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
/*global module, require, Promise, console */ | |
const aws = require("aws-sdk"); | |
const fs = require("fs"); | |
const s3 = new aws.S3(); | |
const downloadFileFromS3 = function (bucket, fileKey, filePath) { | |
"use strict"; | |
console.log("Downloading", bucket, fileKey, filePath); | |
return new Promise(function (resolve, reject) { | |
const file = fs.createWriteStream(filePath), | |
stream = s3 | |
.getObject({ | |
Bucket: bucket, | |
Key: fileKey, | |
}) | |
.createReadStream(); | |
stream.on("error", reject); | |
file.on("error", reject); | |
file.on("finish", function () { | |
console.log("downloaded", bucket, fileKey); | |
resolve(filePath); | |
}); | |
stream.pipe(file); | |
}); | |
}; | |
module.exports = { | |
downloadFileFromS3: downloadFileFromS3, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment