-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoss.js
106 lines (99 loc) · 2.89 KB
/
oss.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
const OSS = require('ali-oss')
const Globby = require('globby')
const { ossPath, localPath, accessKeyId, accessKeySecret, bucket, region } = require('./config')
const client = OSS({
accessKeyId,
accessKeySecret,
bucket,
region
})
// 清空OSS目录下的文件
const deleteMulti = async (arr) => {
return new Promise(async (resolve, rejected) => {
try {
const result = await client.deleteMulti(arr, {
quiet: true
})
if (result.res.statusCode === 200) {
resolve(true)
} else {
resolve(false)
}
} catch (e) {
console.log('❌删除接口错误,请重新运行~', e)
rejected(e)
}
})
}
// 获取本地文件目录树
const localFilePath = async (path) => {
return new Promise(async (resolve) => {
const paths = await Globby(path)
resolve(paths)
})
}
// 文件上传
const put = async (localFilePath) => {
return new Promise(async (resolve, rejected) => {
try {
const ossFilePath = `${ossPath}${localFilePath.substring(localPath.length, localFilePath.length)}`
const result = await client.put(ossFilePath, localFilePath)
resolve(result.res.statusCode === 200 ? true : false)
} catch (e) {
console.log('❌文件上传错误,请重新运行~', e)
rejected(e)
}
})
}
// 上传文件方法
const onPut = async (paths) => {
return new Promise(async (resolve) => {
let needPutPaths = []
let index = 0
for (let i of paths) {
const status = await put(i)
if (!status) {
needPutPaths.push(i)
}
console.log(status ? `${i}【✔上传成功~】` : `${i}【❌上传失败~】`)
index += 1
if (index === paths.length) {
resolve(needPutPaths)
}
}
})
}
const next = async () => {
try {
// 不带任何参数,默认最多返回1000个文件。
let result = await client.list({
prefix: ossPath,
marker: ossPath
})
// 判断是否有文件
if (result.objects !== undefined) {
const ossFiles = result.objects.map(e => e.name)
console.log(ossFiles)
if (ossFiles.length) {
const deleteResult = await deleteMulti(ossFiles)
if (!deleteResult) {
console.log('❌文件夹清空失败,请重新运行~')
return
}
}
console.log(ossFiles.length ? '✔文件夹清空成功~' : '✔OSS无文件目录,不执行删除~')
} else {
console.log('✔OSS无目录,继续执行上传~')
}
const paths = await localFilePath(localPath)
if (!paths.length) {
console.log(`${localPath},本地无此目录或目录下无文件,请检查~`)
return
}
const needPutPaths = await onPut(paths)
console.log(`✔脚本执行完毕${needPutPaths.length ? `,请手动上传下面的文件~${needPutPaths}` : ''}`)
} catch (e) {
console.log('❌脚本执行错误,请重新运行~', e)
}
}
next()