-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (78 loc) · 3.13 KB
/
index.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
const fs = require('fs');
const ForgeOss = require('./forge-oss');
const inquirer = require('inquirer');
const settings = require('./settings.json');
const activeSetttins = settings[settings.active];
const dataFolderName = 'data';
async function getFiles(path) {
const entries = await fs.readdirSync(path, { withFileTypes: true });
const files = entries
.filter(file => !file.isDirectory())
.map(file => ({ ...file, path: path + file.name }));
const folders = entries.filter(folder => folder.isDirectory());
for (const folder of folders)
files.push(...await getFiles(`${path}${folder.name}/`));
return files;
}
async function chooseBuckets(buckets) {
var choices = ['All buckets'];
choices.push(...buckets);
const bucketQuestions = [{
type: 'checkbox',
choices: choices,
name: 'BucketKey',
message: 'BucketKey: '
}];
const bucketAnswers = await inquirer.prompt(bucketQuestions);
if (bucketAnswers.BucketKey[0] === 'All buckets')
bucketAnswers.BucketKey = buckets;
return bucketAnswers.BucketKey;
}
async function main() {
const oss = new ForgeOss();
const actionQuestions = [
{
type: 'list',
choices: ['Download', 'Upload'],
name: 'Action',
message: 'Action: '
}
];
const action = await inquirer.prompt(actionQuestions);
console.log('Authenticate to OSS...');
await oss.auth(activeSetttins.client_id, activeSetttins.client_secret);
if (action.Action === 'Download')
{
const buckets = await oss.getBuckets();
const selectedBuckets = await chooseBuckets(buckets);
for (bucketKeyIndex in selectedBuckets) {
const bucketKey = selectedBuckets[bucketKeyIndex];
console.log(`Downloading data from bucket ${bucketKey}`);
var objects = await oss.getObjects(bucketKey);
for (objectIndex in objects) {
const object = objects[objectIndex];
console.log(`Getting object ${object}`);
await oss.getObject(bucketKey, object, `${dataFolderName}/${bucketKey}/${object}`);
}
}
}
if (action.Action === 'Upload') {
const buckets = fs.readdirSync('data');
const selectedBuckets = await chooseBuckets(buckets);
for (bucketIndex in selectedBuckets) {
const bucketKey = selectedBuckets[bucketIndex];
const files = await getFiles(`${dataFolderName}/${bucketKey}/`);
console.log(`Creating bucket ${bucketKey}`);
oss.createBucket(bucketKey);
for (fileIndex in files) {
const file = files[fileIndex];
const filePath = file.path;
const objectKey = filePath.replace(`${dataFolderName}/${bucketKey}/`, '');
console.log(`Uploading object ${objectKey}`);
await oss.putObject(bucketKey, objectKey, filePath);
}
}
}
console.log('Done.');
}
main();