forked from alibaba/ice
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync-db.js
More file actions
47 lines (39 loc) · 1.07 KB
/
sync-db.js
File metadata and controls
47 lines (39 loc) · 1.07 KB
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
const oss = require('ali-oss');
const co = require('co');
const { readdirSync } = require('fs');
const { resolve, join } = require('path');
const bucket = 'iceworks';
const accessKeyId = process.env.ACCESS_KEY_ID;
const accessKeySecret = process.env.ACCESS_KEY_SECRET;
const store = oss({
bucket,
endpoint: 'oss-cn-hangzhou.aliyuncs.com',
accessKeyId,
accessKeySecret,
time: '120s',
});
console.log('start uploading');
const files = readdirSync(resolve(__dirname, '../build')).map((filename) => ({
from: resolve(__dirname, '../build', filename),
to: join('assets', filename),
}));
console.log(files);
function createUploadTask(opts) {
const { from, to } = opts;
return co(store.put(to, from)).then((object = {}) => {
if (object.res && object.res.status == 200) {
console.log('upload ok', object.url);
return true;
} else {
throw new Error('upload err:' + to);
}
});
}
const tasks = files.map(createUploadTask);
Promise.all(tasks)
.then(() => {
console.log('All Done');
})
.catch((err) => {
console.log('upload err', err);
});