Skip to content

Instantly share code, notes, and snippets.

@batrudinych
Last active January 9, 2019 10:26
Show Gist options
  • Save batrudinych/fbea6700d495bec171dccafb0da75d1c to your computer and use it in GitHub Desktop.
Save batrudinych/fbea6700d495bec171dccafb0da75d1c to your computer and use it in GitHub Desktop.
[s3-redeploy]-parallel-files-uploading
// Limit of promises executed in parallel
// We don't want to upload more than 3 files simultaneously
const concurrency = 3;
// Returns a promise, which means uploading is fired once function is called
function uploadSingleFileToS3(fileName) {
const fileStream = fs.createReadStream(fileName);
return s3Client.upload({
Key: fileName,
Body: fileStream,
}).promise();
}
// Perform parallel uploading applying concurrency limit
function uploadFilesToS3(fileNames) {
// The function, given as a second parameter, should return a promise
return parallel(
fileNames,
uploadSingleFileToS3,
concurrency
);
}
// Raw idea of what is done inside
// Returns a promise
function parallel(values, fn, concurrency = 1) {
// Take 'concurrency' first values and apply 'fn' to each.
// 'concurrency' files uploading will be fired simultaneously and
// 'fn' will return a promise for each call.
// Chain the following to each returned promise:
// - take a value from 'values'
// - apply 'fn'
// - chain again
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment