-
Notifications
You must be signed in to change notification settings - Fork 14
/
s3_util.js
124 lines (102 loc) · 3.39 KB
/
s3_util.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const AWS = require('aws-sdk');
const AdmZip = require('adm-zip');
const path = require('path');
const BUCKET_NAME = 'iden3-circuits-bucket';
const FILES_TO_INCLUDE = ['circuit.wasm', 'circuit_final.zkey', 'verification_key.json'];
const FOLDER_TO_COMPRESS = './build';
const OPERATIONS = ['add', 'rm', 'zip'];
function main() {
const [operation, zipName] = parseArgs();
processOperation(operation, zipName);
}
main();
function processOperation(operation, zipName) {
try {
switch (operation) {
case 'add': uploadZipFile(zipName)
break;
case 'rm': deleteFile(zipName)
break;
case 'zip':
const zip = makeZip(zipName)
console.log(`Starting saving zip...`);
zip.writeZip(zipName);
console.log(`Zip successfully saved: ${path.join(process.cwd(), zipName)}`);
break;
default:
console.error(`Operation ${operation} is not supported`)
break;
}
} catch (error) {
console.error(error)
process.exit(1)
}
}
function parseArgs() {
let [operation, zipName] = process.argv.slice(2);
if (!OPERATIONS.includes(operation)) {
console.error(`Error! Supported operations: ${OPERATIONS.join(', ')}`);
process.exit(1)
}
if (!zipName) {
console.error(`please provide name for zip file as argument, for example 'node s3_upload.js add v1.zip'`)
process.exit(1)
}
zipName = !zipName.includes('.zip') ? `${zipName}.zip` : zipName;
return [operation, zipName];
};
function uploadZipFile(zipName) {
const s3 = new AWS.S3({
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY
});
const zip = makeZip()
console.log('Creating zip buffer...');
const params = {
Bucket: BUCKET_NAME,
Key: zipName,
Body: zip.toBuffer()
};
console.log('Buffer successfully created')
// Uploading files to the bucket
const uploading = s3.upload(params, function (err, data) {
if (err) {
console.error(err.message ?? err);
throw err;
}
console.info(`Zip file uploaded successfully. ${data.Location}`);
});
trackProgressFor(uploading);
};
function makeZip() {
console.info(`Starting creation of zip file...`);
const zip = new AdmZip();
zip.addLocalFolder(FOLDER_TO_COMPRESS, '', name => FILES_TO_INCLUDE.some(ext => name.endsWith(ext)));
return zip;
}
function deleteFile(zipName) {
console.info(`Starting deleting of ${zipName} from ${BUCKET_NAME}...`);
const s3 = new AWS.S3({
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY
});
// Setting up S3 upload parameters
const params = {
Bucket: BUCKET_NAME,
Key: zipName
};
// Uploading files to the bucket
const deletion = s3.deleteObject(params, function (err, data) {
if (err) {
console.error(err.message ?? err);
throw err;
}
console.log(`File ${zipName} successfully deleted.`);
});
trackProgressFor(deletion);
}
function trackProgressFor(source) {
source.on('httpUploadProgress', ({ loaded, total }) => {
if (loaded && total) console.log(`Progress: ${Math.floor(loaded / total * 100)}%`);
});
}