-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.bak.js
82 lines (73 loc) · 2.36 KB
/
upload.bak.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
const OSS = require("ali-oss");
const path = require("path");
const fs = require("fs");
const localPath = "./build/";
const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
const remotePath = `friday`;
const client = new OSS({});
function readDir(currentDirPath, callback) {
fs.readdir(currentDirPath, function (err, files) {
if (err) {
throw new Error(err);
}
files.forEach(function (name) {
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile()) {
callback(filePath, stat);
} else if (stat.isDirectory()) {
readDir(filePath, callback);
}
});
});
}
async function upload(file) {
try {
// 填写Object完整路径和本地文件的完整路径。Object完整路径中不能包含Bucket名称。
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
let formatRile = path.normalize(file).replace(/\\/g, "/");
let newPath = formatRile.replace(/build/g, "");
newPath = newPath.replace(/\//g, "/");
const remotePaths = remotePath + newPath;
const result = await client.put(remotePaths, formatRile);
if (result.res.status === 200) {
console.log("UPLOAD SUCCESS:" + packageJSON.version + "+" + file);
// 上传成功后删除
// fs.unlinkSync(file);
}
} catch (e) {
console.log(e);
}
}
// 上传单个文件
async function uploadSingleFile(file, remotefileName) {
try {
// 填写Object完整路径和本地文件的完整路径。Object完整路径中不能包含Bucket名称。
// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
const remotePaths = remotePath + remotefileName;
const result = await client.put(remotePaths, file);
if (result.res.status === 200) {
console.log("UPLOAD SUCCESS:" + file);
// 上传成功后删除
fs.unlinkSync(file);
}
} catch (e) {
console.log(e);
}
}
async function deleteAll () {
const result = await client.list({
prefix: remotePath
});
result.objects.forEach(item => {
console.log("DELETE SUCCESS:" + item.name);
client.delete(item.name);
});
uploadAllFile();
}
function uploadAllFile() {
readDir(localPath, function (filePath, stat) {
upload(filePath);
});
}
deleteAll();