-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployToTencentCos.js
56 lines (51 loc) · 1.42 KB
/
deployToTencentCos.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
var COS = require('cos-nodejs-sdk-v5');
const cosConfig = require('./config')
const fs = require('fs');
const path = require('path');
var cos = new COS({
SecretId: cosConfig.tencentCos.SecretId,
SecretKey: cosConfig.tencentCos.SecretKey
});
/**
* 遍历指定目录下的所有文件
* @param {*} dir
*/
const getAllFile = (dir) => {
return new Promise((resolve, reject) => {
let res = []
function traverse (dir) {
fs.readdirSync(dir).forEach((file) => {
const pathname = path.join(dir, file)
if (fs.statSync(pathname).isDirectory()) {
traverse(pathname)
} else {
res.push(pathname)
}
})
}
traverse(dir);
resolve(res);
});
}
const putCos = (dir, files) => {
for (let i = 0; i < files.length; i++) {
const file = files[i]
const target = `${file}`.replace(dir, '')
cos.putObject({
Bucket: cosConfig.tencentCos.Bucket, /* 必须 */
Region: cosConfig.tencentCos.Region, /* 必须 */
Key: target, /* 必须 */
StorageClass: 'STANDARD',
Body: fs.createReadStream(file), // 上传文件对象
// onProgress: function(progressData) {
// console.log(JSON.stringify(progressData));
// }
}, function(err, data) {
// console.log(err || data);
});
}
}
const dir = 'docs'
getAllFile(dir).then((files) => {
putCos(dir, files);
})