Created
September 1, 2017 10:07
-
-
Save muthuspark/5949e24fe19feb0c363677385185538a to your computer and use it in GitHub Desktop.
Node JS based code to upload folders into AWS S3 using aws-sdk
This file contains 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
var q = require('q'); | |
var AWS = require('aws-sdk'); | |
AWS.config.update({ | |
accessKeyId: "131231", | |
secretAccessKey: "23135124" | |
}); | |
// Create an S3 client | |
var s3 = new AWS.S3(); | |
// Create a bucket and upload something into it | |
var bucketName = 'bucket_name'; | |
var S3Url = "https://s3-us-west-2.amazonaws.com/bucket_name"; | |
var walkSync = function(dir, filelist) { | |
var fs = fs || require('fs'), | |
files = fs.readdirSync(dir); | |
filelist = filelist || []; | |
files.forEach(function(file) { | |
if (fs.statSync(dir + file).isDirectory()) { | |
filelist = walkSync(dir + file + '/', filelist); | |
} | |
else { | |
filelist.push(dir+file); | |
} | |
}); | |
return filelist; | |
}; | |
var files = walkSync('_path_to_folder_that_needs_to_be_uploaded'); | |
// for each file in the directory | |
for (const fileName of files) { | |
// read file contents | |
fs.readFile(fileName, (error, fileContent) => { | |
// if unable to read file contents, throw exception | |
if (error) { throw error; } | |
s3.putObject({ | |
ACL: 'public-read', | |
Bucket: bucketName, | |
Key: '_path_of_directory_under_s3_bakcet_where_youwant_to_upload/'+fileName.replace('_parent_path_from_folder_to_be_uploaded',''), //the directory comes with the absolute path, you may want to remove the prefix. | |
Body: fileContent | |
}, function(error, response) { | |
console.log(`Successfully uploaded '${fileName}'!`,response); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment