Skip to content

Instantly share code, notes, and snippets.

@cpsubrian
Last active December 2, 2018 22:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsubrian/3004adbc9b4966e8605de343ca3ecc58 to your computer and use it in GitHub Desktop.
Save cpsubrian/3004adbc9b4966e8605de343ca3ecc58 to your computer and use it in GitHub Desktop.
Upload files to S3 via node aws-sdk (if aws-cli isn't easily installable)
// Put credentials in ~/.aws/credentials like:
//
// [default]
// aws_access_key_id = [key]
// aws_secret_access_key = [secret]
if (process.argv.length !== 5) {
console.log('Usage: node ./upload.js [filepath] [bucket] [key]')
process.exit()
}
var fs = require('fs')
var AWS = require('aws-sdk')
var fileStream = fs.createReadStream(process.argv[2])
fileStream.on('error', function (err) {
if (err) { throw err }
})
fileStream.on('open', function () {
var s3 = new AWS.S3()
var req = s3.putObject({
Bucket: process.argv[3],
Key: process.argv[4],
Body: fileStream
}, function (err) {
if (err) { throw err }
})
req.on('httpUploadProgress', function(progress) {
console.log('Uploaded ' +
progress.loaded.toLocaleString() +
' ' +
'(' +
((progress.loaded / progress.total) * 100) + '%' +
')'
)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment