Skip to content

Instantly share code, notes, and snippets.

@nidev
Created February 25, 2018 07:26
Show Gist options
  • Save nidev/af9acbf0b38e21305f9556f5004ef3b6 to your computer and use it in GitHub Desktop.
Save nidev/af9acbf0b38e21305f9556f5004ef3b6 to your computer and use it in GitHub Desktop.
Recursively upload all content from given path(s)
"use strict";
/*
* AWS S3 Bucket automatic deployment script
* 1. Run 'npm install' first to install aws-sdk
* 2. Run 'hexo generate'
* 3. Run this script (Please configure AWS credentials first)
*
*/
const AWS = require("aws-sdk");
const myBucket = ''; //TODO: configure here
const myRegion = ''; //TODO: configure here
const path = require("path");
const fs = require("fs");
let s3 = null;
/**
* Upload all folders and files from given rootDir.
* @param {string} s3KeyPath - Path from S3 Root, should not start with /, optional(default: empty string)
* @param {string} dirPath - Path to be found, reqruied
* @param {[RegExp]} excluding - (STUB) files/folders to be excluded, optional(default: empty array)
*/
function uploadFullTree(s3KeyPath, dirPath, excluding) {
if (!dirPath) {
throw "Can not perform file uploading (empty rootDir)";
}
if (!s3KeyPath) {
s3KeyPath = "";
}
if (!excluding) {
excluding = [];
}
console.log("::> " + dirPath);
fs.readdir(dirPath, (err, files) => {
if (err) {
console.error(dirPath + ": " + err);
return;
}
files.forEach((fileName, index) => {
let localFullPath = path.join(dirPath, fileName);
console.log("::::> " + localFullPath);
if (fileName.startsWith(".")) {
console.error(localFullPath +": Skipped");
return;
}
fs.lstat(localFullPath, (err, stats) => {
if (err) {
console.error(localFullPath + ": " + err);
return;
}
let s3Key = fileName;
if (s3KeyPath !== "") {
s3Key = s3KeyPath + "/" + fileName;
}
if (stats.isFile()) {
// Generate key, and perform upload here
let params = {
Bucket: myBucket,
Key: s3Key,
Body: fs.createReadStream(localFullPath)
};
s3.upload(params, (err, data) => console.log(err, data));
} else if (stats.isDirectory()) {
uploadFullTree(s3Key, localFullPath, excluding);
} else {
console.log(s3Key + ": " + "Folder walking skipped");
}
});
});
});
}
/**
* Main function of this script
*/
function main() {
AWS.config.update({region: myRegion});
if (process.argv.length < 3) {
console.error("s3deploytool.js - Upload recursively all folders files");
console.error("How to invoke:");
console.error(" $ node s3deploytool.js path1 path2 path3...");
process.exit(-1);
}
s3 = new AWS.S3();
s3.createBucket({Bucket: myBucket}, (err, data) => {
if (err) {
console.log(err);
} else {
process.argv.slice(2).forEach((path, index) => {
uploadFullTree("", path, []);
});
}
});
}; main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment