-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutipart_glacier.js
76 lines (62 loc) · 2.18 KB
/
mutipart_glacier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
const aws = require('aws-sdk')
let treehash = require('treehash')
const fs = require('fs')
const path = require('path')
let glacier = new aws.Glacier({apiVersion: '2012-06-01',region:'ap-southeast-1'})
let myarg = process.argv.slice(2)
let vaultName = myarg[0]
let filePath = myarg[1]
let partSize = 512 * 1024 * 1024
console.log("Calculating hash. Takes some time.....")
let fileName = path.basename(filePath)
let {size:filesize} = fs.statSync(filePath)
let fd = fs.openSync(filePath,'r')
let thStream = treehash.createTreeHashStream ();
let fileStream = fs.createReadStream(filePath);
fileStream.on('data', function(chunk) {
thStream.update(chunk);
});
fileStream.on('end', function (){
let sha = thStream.digest()
disk(sha)
});
async function disk(sha){
try {
let numParts = Math.ceil(filesize / partSize)
console.log('Initiating upload to', vaultName);
let init = glacier.initiateMultipartUpload({
vaultName: vaultName,
partSize: partSize.toString(),
archiveDescription: fileName
}).promise()
let multipart = await init
console.log("Got upload ID", multipart.uploadId)
let current = 0
for (let i = 0; i < numParts; i++) {
let buffer = Buffer.alloc(partSize);
let read = fs.readSync(fd, buffer, 0, partSize, current)
let body = buffer.slice(0, read)
console.log(`Uploading part ${i+1}/${numParts}`)
let upload = glacier.uploadMultipartPart({
vaultName: vaultName,
uploadId: multipart.uploadId,
range: `bytes ${current}-${current + read - 1}/*`,
body
}).promise()
let mdata = await upload
current += partSize
}
console.log("Completing upload...");
let final = glacier.completeMultipartUpload({
vaultName: vaultName,
uploadId: multipart.uploadId,
archiveSize: filesize.toString(),
checksum: sha
}).promise()
let data = await final
console.log('Archive ID:', data.archiveId);
}catch (e) {
console.error(e)
}
}