Skip to content

Instantly share code, notes, and snippets.

@RadoslawB
Last active July 19, 2021 10:49
Show Gist options
  • Save RadoslawB/efec5f5137cb705d7b241c88e0140621 to your computer and use it in GitHub Desktop.
Save RadoslawB/efec5f5137cb705d7b241c88e0140621 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const { promisify } = require('util');
const uploadFiles = (directoryPath, s3client) => promisify(fs.readdir)(directoryPath)
.then(files => files.map(fileName => getUploadFileOptions(fileName, directoryPath, AWS_S3_BUCKET_NAME)))
.then(uploadOptions => Promise.all(uploadOptions.map(option => uploadFile(option, s3client))))
.catch(e => {
console.log('Failed to upload files');
throw e;
});
const getUploadFileOptions = (fileName, uploadDirectoryPath, bucketName) => ({
Bucket: bucketName,
Key: fileName,
Body: fs.readFileSync(path.join(uploadDirectoryPath, fileName)),
ContentType: 'text/html',
ACL: 'public-read'
});
const uploadFile = (options, s3client) => s3client.upload(options).promise()
.then(data => {
console.log('Uploaded', data.Key);
return data;
})
.catch(err => {
console.log('Falied do upload', err);
throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment