Last active
July 12, 2022 23:16
-
-
Save ahmedbaig/8862bfae0cbd64e38195beffa3f6a1f6 to your computer and use it in GitHub Desktop.
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
// Dynamic Flags | |
let args = {};process.argv.forEach(function (val, index, array) {let valsplit = val.split("=");args[valsplit[0]] = valsplit[1]}); | |
// Speedometer | |
function formatBytes(a,b=2,k=1024){with(Math){let d=floor(log(a)/log(k));return 0==a?"0 Bytes":parseFloat((a/pow(k,d)).toFixed(max(0,b)))+" "+["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][d]}} | |
var fs = require('fs'); | |
var AWS = require('aws-sdk'); | |
const StreamSpeed = require('streamspeed'); | |
// File | |
if(args['--filename'] == null){ | |
console.log("File name not specified \n Hint: Use `--filename=` to specify the file to be uploaded") | |
process.exit(1) | |
} | |
var fileName = args['--filename']; | |
var filePath = './' + fileName; | |
if(args['--bucket'] == null){ | |
console.log("Bucket name not specified \n Hint: Use `--bucket=` to specify the bucket to upload to") | |
process.exit(1) | |
} | |
if(args['--region'] == null){ | |
console.log("Region name not specified \n Hint: Use `--region=` to specify the bucket region to upload to") | |
process.exit(1) | |
} | |
const REGION = args['--region']; | |
var s3 = new AWS.S3({ | |
accessKeyId: process.env.AWS_S3_IAM_USER_KEY, | |
secretAccessKey: process.env.AWS_S3_IAM_USER_SECRET, | |
Bucket: args['--bucket'], | |
}); | |
// S3 Upload options | |
var bucket = args['--bucket']; | |
// node largeUpload.js --filename=<Something Big> --bucket=<bucket> --region=<AWS Region> | |
// ---------------------------------------------------------------------------------------- CONFIGS | |
var uploadParams = {Bucket: bucket, Key: '', Body: ''}; | |
var fileStream = fs.createReadStream(fileName); | |
let ss = new StreamSpeed(); | |
ss.add(fileStream); | |
let size = fs.statSync(filePath).size; | |
let humanReadableSize = formatBytes(size) | |
let prog = 0; | |
fileStream.on('data', function(d){ | |
prog += d.length | |
}) | |
ss.on('speed', (speed) => { | |
process.stdout.clearLine(1); | |
process.stdout.cursorTo(0); | |
process.stdout.write(` 📁 ${fileName} - (${humanReadableSize}) | 📤 Upload Progress: ${Number((prog / size) * 100).toFixed(0)} % - (${formatBytes(prog)}) | 🚀 Speed: ${formatBytes(speed)}/s `); | |
}); | |
fileStream.on('end', function(){ | |
process.stdout.write(`\n`); | |
}) | |
fileStream.on('error', function(err) { | |
console.log('File Error', err); | |
}); | |
uploadParams.Body = fileStream; | |
var path = require('path'); | |
uploadParams.Key = path.basename(fileName); | |
// call S3 to retrieve upload file to specified bucket | |
s3.upload (uploadParams, function (err, data) { | |
if (err) { | |
console.log("Error", err); | |
} if (data) { | |
console.log("Upload Success:", data.Location); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment