-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish-cos.js
107 lines (98 loc) · 2.79 KB
/
publish-cos.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
const COS = require("cos-nodejs-sdk-v5");
const fs = require("fs");
const path = require("path");
const cos = new COS({
SecretId: process.env.TENCENT_SECRET_ID,
SecretKey: process.env.TENCENT_SECRET_KEY,
});
// 递归获取目录中的所有文件
const getAllFiles = (dir, prefix) => {
let results = [];
const list = fs.readdirSync(dir);
list.forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory()) {
// 递归子目录
results = results.concat(getAllFiles(filePath, path.join(prefix, file)));
} else {
results.push({
Key: path.join(prefix, file),
FilePath: filePath,
});
}
});
return results;
};
const uploadDir = async (dir, bucket, region, prefix) => {
const files = getAllFiles(dir, prefix).map((file) => ({
Bucket: bucket,
Region: region,
Key: file.Key,
FilePath: file.FilePath,
}));
console.log('waiting to check', files.length);
const getETag = (filePath) => {
const data = fs.readFileSync(filePath);
const hash = require("crypto").createHash("md5").update(data).digest("hex");
return hash;
};
const checkFileHead = (file) => {
return new Promise((resolve, reject) => {
cos.headObject(
{
Bucket: file.Bucket,
Region: file.Region,
Key: file.Key,
},
(err, headResponse) => {
if (err) {
// 如果 HTTP 状态码为 404,表示 COS 中不存在该文件
if (err.statusCode === 404) {
resolve(file);
} else {
console.error("Error checking file:", file.Key, err);
reject(err);
}
} else if (headResponse.ETag !== '"' + getETag(file.FilePath) + '"') {
resolve(file);
} else {
resolve(null);
}
}
);
});
};
const filesToUploadPromises = files.map(checkFileHead);
let filesToUpload = await Promise.all(filesToUploadPromises);
filesToUpload = filesToUpload.filter((file) => file !== null);
if (filesToUpload.length > 0) {
console.log("waiting to upload:", filesToUpload.length);
// 使用 uploadFiles 方法批量上传
cos.uploadFiles(
{
files: filesToUpload,
SliceSize: 1024 * 1024, // 1MB
onProgress: (progressData) => {
console.log(JSON.stringify(progressData));
},
async: true,
},
(err, data) => {
if (err) {
console.error(err);
} else {
console.log("上传完成:", data);
}
}
);
} else {
console.log("所有文件均已存在且未更改,无需上传。");
}
};
uploadDir(
"public",
process.env.COS_BUCKET,
process.env.COS_REGION,
process.env.COS_UPLOAD_PATH
);