Created
December 11, 2019 08:55
-
-
Save bachue/b7a1f8897504936b0294567a1bc6c635 to your computer and use it in GitHub Desktop.
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
const AWS = require('aws-sdk'), | |
fs = require('fs'), | |
{ | |
Throttle | |
} = require('stream-throttle'); | |
const uploadFiles = process.argv.slice(2); | |
const S3Service = new AWS.S3({ | |
apiVersion: '2006-03-01', | |
computeChecksums: true, | |
logger: console, | |
endpoint: 'https://s3-cn-east-1.qiniucs.com', | |
region: 'cn-east-1', | |
accessKeyId: 'QINIU-AK', | |
secretAccessKey: 'QINIU-SK', | |
maxRetries: 3, | |
s3ForcePathStyle: true, | |
signatureVersion: 'v4', | |
httpOptions: { | |
connectTimeout: 3000, // 3s | |
timeout: 300000 // 5m | |
} | |
}); | |
uploadFiles.forEach((filePath) => { | |
const uploader = new AWS.S3.ManagedUpload({ | |
service: S3Service, | |
params: { | |
Bucket: 'z0-bucket', | |
Key: 'test', | |
Body: createReadStream(filePath, 1024 * 1024 * 100), | |
}, | |
partSize: 8 * 1024 * 1024, | |
queueSize: 10 | |
}); | |
console.info('Upload:', filePath); | |
uploader.on('httpUploadProgress', (prog) => { | |
console.info('Progress:', prog.loaded, '/', prog.total); | |
}); | |
uploader.send((err, data) => { | |
console.info('Uploaded:', filePath); | |
if (err) { | |
console.error('Error:', err); | |
} | |
}); | |
}); | |
function createReadStream(localFile, limit) { | |
const readStream = fs.createReadStream(localFile); | |
if (limit) { | |
const streamWithThrottle = readStream.pipe(new Throttle({rate: limit})).on('finish', () => { | |
console.log('************ finished'); | |
}); | |
streamWithThrottle.path = readStream.path; | |
return streamWithThrottle; | |
} else { | |
return readStream; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment