-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathossUploadDist.js
56 lines (45 loc) · 1.44 KB
/
ossUploadDist.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
const fs = require('fs');
const path = require('path');
const OSS = require('ali-oss');
// 阿里云 OSS 的 AccessKey ID 和 AccessKey Secret
const accessKeyId = 'aaaaaaaaaaaaaaaaa';
const accessKeySecret = 'bbbbbbbbbbbbbbbb';
// 阿里云 OSS 的 Endpoint、Bucket 和 Region
const bucket = 'bucket';
const region = `oss-cn-beijing`;
const endpoint = `http://${region}.aliyuncs.com`;
// 创建 OSS 实例
const client = new OSS({
accessKeyId,
accessKeySecret,
endpoint,
bucket,
region,
});
// 调用函数上传 dist 目录下的所有文件和子目录
const localDirPath = 'C:\\Users\\Easy\\Desktop\\middlwPlatform\\sp-platform\\dist\\';
// const localDirPath = path.join(__dirname, 'dist');
async function uploadFiles(dirPath, prefix = '') {
const files = await fs.promises.readdir(dirPath);
for (const file of files) {
const filePath = path.join(dirPath, file);
const key = prefix ? `${prefix}/${file}` : file;
const stats = await fs.promises.stat(filePath);
if (stats.isFile()) {
// 上传文件
await client.put(key, filePath);
} else if (stats.isDirectory()) {
// 上传子目录
await uploadFiles(filePath, key);
}
}
}
async function uploadDist(distPath) {
try {
await uploadFiles(distPath);
console.log('上传成功!');
} catch (err) {
console.error('上传失败:', err);
}
}
uploadDist(localDirPath);