-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathupload-qiniu.js
119 lines (109 loc) · 3.35 KB
/
upload-qiniu.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
'use strict';
const fs = require('fs');
const path = require('path');
const qiniu = require('qiniu');
const argv1 = process.argv.splice(2);
const accessKey = (argv1.filter(e => e.includes('--accessKey='))[0] || '').split('--accessKey=')[1];
const secretKey = (argv1.filter(e => e.includes('--secretKey='))[0] || '').split('--secretKey=')[1];
const bucket = (argv1.filter(e => e.includes('--bucket='))[0] || '').split('--bucket=')[1];
if (!(accessKey && secretKey && bucket)) throw new Error('参数缺省');
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
const prefix = fs.readFileSync(path.resolve(__dirname, './src/config/prefix'), 'utf-8');
console.log(`prefix: ${prefix}`);
//自定义凭证有效期(示例2小时,expires单位为秒,为上传凭证的有效时间)
const options = {
scope: bucket,
expires: 7200,
};
const putPolicy = new qiniu.rs.PutPolicy(options);
const uploadToken = putPolicy.uploadToken(mac);
const config = new qiniu.conf.Config();
// 空间对应的机房
config.zone = qiniu.zone.Zone_z2;
// 是否使用https域名
//config.useHttpsDomain = true;
// 上传是否使用cdn加速
config.useCdnDomain = true;
const formUploader = new qiniu.form_up.FormUploader(config);
const putExtra = new qiniu.form_up.PutExtra();
const _filesAll = [];
/**
* 遍历目录返回结构
* @param targetPath
* @returns {{path: *, dirsData: [], files: []}}
*/
function readDir(targetPath) {
const filesAll = fs.readdirSync(targetPath);
const dirs = [];
const files = [];
let dirsData = [];
filesAll.filter(file => {
const status = fs.statSync(path.join(targetPath, file));
status.isDirectory() ? dirs.push(file) : files.push(file);
});
dirsData = dirs.map(dir => readDir(path.join(targetPath, dir)));
const key = targetPath.replace(/.*dist/, prefix).replace(/\\/g, '/');
files.forEach(e => {
_filesAll.push({
path: path.join(targetPath, e),
key: `${key}/${e}`,
});
});
return {
path: targetPath,
key,
files,
dirsData,
};
}
// 文件上传
const putFile = function (key, localFile) {
return new Promise((resolve, reject) => {
let again = true;
const push = function () {
formUploader.putFile(uploadToken, key, localFile, putExtra, function (respErr, respBody, respInfo) {
if (respErr) {
// 失败1秒后重试一次
if (again) {
again = false;
setTimeout(() => {
push();
}, 1000);
} else {
reject(respErr);
throw respErr;
}
}
if (respInfo.statusCode === 200) {
console.log(`${localFile} 上传成功`);
console.log(respBody);
resolve(respBody);
} else {
console.log(respInfo.statusCode);
console.log(respBody);
reject(respInfo.statusCode, respBody);
}
});
};
push();
});
};
(async function () {
try {
await readDir(path.resolve(__dirname, './dist'));
console.log('upload-qiniu-cdn-start!!!');
const putFilePromiseAll = [];
for (let i = 0; i < _filesAll.length; i++) {
putFilePromiseAll.push(putFile(_filesAll[i].key, _filesAll[i].path));
}
Promise.all(putFilePromiseAll)
.then(() => {
console.log('upload-qiniu-cdn-done!!!');
})
.catch(err => {
throw err;
});
} catch (e) {
throw e;
}
})();