Last active
January 17, 2018 03:23
-
-
Save drkdelaney/739832d434718fbbfb7be8feef504dc2 to your computer and use it in GitHub Desktop.
Uploads a folder and its content to an s3 bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get variables from command line | |
*/ | |
let cmd = {}; | |
let args = ['command', 'file', 'bucket', 'src'] | |
process.argv.forEach(function(val, index, array) { | |
if (array.length === 4) { | |
cmd[args[index]] = val; | |
} | |
}); | |
/** | |
* Upload using aws-sdk | |
*/ | |
const AWS = require("aws-sdk"); // from AWS SDK | |
const fs = require("fs"); // from node.js | |
const path = require("path"); // from node.js | |
// configuration | |
const config = { | |
s3BucketName: cmd.bucket, | |
folderPath: cmd.src | |
}; | |
// initialize S3 client | |
const s3 = new AWS.S3({ signatureVersion: "v4" }); | |
// resolve full folder path | |
const dist = path.join(__dirname, config.folderPath); | |
uploadFiles(dist); | |
function uploadFiles(distFolderPath, root='') { | |
// get of list of files from 'dist' directory | |
fs.readdir(distFolderPath, (err, files) => { | |
if (!files || files.length === 0) { | |
console.log(`provided folder '${distFolderPath}' is empty or does not exist.`); | |
console.log("Make sure your project was compiled!"); | |
return; | |
} | |
// for each file in the directory | |
for (const fileName of files) { | |
// get the full path of the file | |
const filePath = path.join(distFolderPath, fileName); | |
// create new root if its a directory | |
if (fs.lstatSync(filePath).isDirectory()) { | |
const newRoot = path.join(root, fileName); | |
uploadFiles(filePath, newRoot); | |
continue; | |
} | |
// read file contents | |
fs.readFile(filePath, (error, fileContent) => { | |
// if unable to read file contents, throw exception | |
if (error) { | |
throw error; | |
} | |
const fileKey = path.join(root,fileName); | |
// upload file to S3 | |
s3.putObject( | |
{ | |
Bucket: config.s3BucketName, | |
Key: fileKey, | |
Body: fileContent | |
}, | |
res => { | |
console.log(`Successfully uploaded '${fileKey}'!`); | |
} | |
); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use node command to run
node s3_upload.js bucket_name src_folder