Skip to content

Instantly share code, notes, and snippets.

@pacarvalho
Created April 17, 2022 14:37
Show Gist options
  • Save pacarvalho/b2fdd6a3df05037021b252d6f70d003b to your computer and use it in GitHub Desktop.
Save pacarvalho/b2fdd6a3df05037021b252d6f70d003b to your computer and use it in GitHub Desktop.
Utility Function to Download Files from S3
/*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