Skip to content

Instantly share code, notes, and snippets.

@stevehanson
Created March 29, 2017 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevehanson/59ade977d68f6e1824d932b167442b18 to your computer and use it in GitHub Desktop.
Save stevehanson/59ade977d68f6e1824d932b167442b18 to your computer and use it in GitHub Desktop.
Node -- Upload directory to S3
// requires following dependencies: yarn add s3 dotenv
var s3 = require('s3');
require('dotenv').config()
var client = s3.createClient({
s3Options: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});
var params = {
localDir: "build",
deleteRemoved: true,
s3Params: {
Bucket: "xxxxxxx",
Prefix: "",
ACL: 'public-read'
// other options supported by putObject, except Body and ContentLength.
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
},
};
console.log('Uploading to S3....')
var uploader = client.uploadDir(params);
uploader.on('error', function(err) {
console.error("unable to sync:", err.stack);
});
uploader.on('progress', function() {
if(uploader.progressAmount && uploader.progressTotal) {
console.log("Uploaded", Math.round(uploader.progressAmount/uploader.progressTotal * 100) + '%' );
}
});
uploader.on('end', function() {
console.log("done uploading");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment