forked from wangsijie/static-deploy-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
put.js
35 lines (34 loc) · 1.07 KB
/
put.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
const fs = require('fs');
const getClient = require('./oss');
const resolveObject = require('./resolve-object');
module.exports = async ({ aliyun, local, remote }) => {
const client = getClient(aliyun);
if (!fs.existsSync(local)) {
throw new Error('local file doesn\'t exists: ' + local);
}
let checkpoint;
let tryTime = 10;
async function asyncProgress(p, cpt) {
checkpoint = cpt;
const progress = Math.floor(p * 100);
console.log(`Uploading: ${local} (${progress}%)`);
}
const upload = async () => {
try {
return await client.multipartUpload(resolveObject(remote), local, {
checkpoint,
progress: asyncProgress
});
} catch (e) {
tryTime--;
if (tryTime && !process.env.DISABLE_RETRY) {
console.log(`Retrying: ${local}`);
await new Promise(r => setTimeout(r, 1000))
await upload();
} else {
throw e;
}
}
}
return await upload();
}