Created
May 22, 2018 15:54
-
-
Save minhoyooDEV/222ea05b55c4966a4fd7a4136742253c to your computer and use it in GitHub Desktop.
upload local file with aws-sdk using node
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
//https://gist.github.com/kethinov/6658166 | |
var AWS = require('aws-sdk'), | |
fs = require('fs'), | |
path = require('path'); | |
// For dev purposes only | |
AWS.config.update({accessKeyId: '.', secretAccessKey: '..'}); | |
// Read in the file, convert it to base64, store to S3 | |
//https://gist.github.com/kethinov/6658166 | |
console.log('====read directory==='); | |
var walkSync = function (dir, filelist) { | |
var fs = fs || require('fs'), | |
files = fs.readdirSync(dir); | |
filelist = filelist || []; | |
files.forEach(function (file) { | |
if (file !== 'node_modules' && file !== '.idea') { | |
if (fs.statSync(dir + '/' + file).isDirectory()) { | |
filelist = walkSync(dir + '/' + file, filelist); | |
} | |
else { | |
filelist.push(`${dir}/${file}`); | |
} | |
} | |
}); | |
return filelist; | |
}; | |
var fileList = [] | |
walkSync(process.argv[2], fileList) | |
console.log(fileList) | |
fileList.forEach((file) => { | |
fs.readFile(file, (err, data) => { | |
if (err) { | |
throw err; | |
} | |
var base64data = new Buffer(data, 'binary'); | |
console.log(file, base64data) | |
var s3 = new AWS.S3(); | |
s3.putObject({ | |
Bucket: 'minho-bucket', | |
Key: file.slice(2), | |
Body: base64data, | |
ACL: 'public-read' | |
},function (resp) { | |
console.log(arguments); | |
console.log('Successfully uploaded package.'); | |
}); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment