2023-02-15 10:36:47 -05:00
|
|
|
import OSS from 'ali-oss'
|
|
|
|
|
2023-04-26 05:01:55 -04:00
|
|
|
interface IConfigMap {
|
|
|
|
fileName: string
|
|
|
|
config: {
|
|
|
|
accessKeyId: string
|
|
|
|
accessKeySecret: string
|
|
|
|
bucket: string
|
|
|
|
area: string
|
|
|
|
path?: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 10:36:47 -05:00
|
|
|
export default class AliyunApi {
|
2023-04-26 05:01:55 -04:00
|
|
|
static async delete (configMap: IConfigMap): Promise<boolean> {
|
2023-02-15 10:36:47 -05:00
|
|
|
const { fileName, config: { accessKeyId, accessKeySecret, bucket, area, path } } = configMap
|
|
|
|
try {
|
|
|
|
const client = new OSS({
|
|
|
|
accessKeyId,
|
|
|
|
accessKeySecret,
|
|
|
|
bucket,
|
|
|
|
region: area
|
|
|
|
})
|
|
|
|
let key
|
|
|
|
if (path === '/' || !path) {
|
|
|
|
key = fileName
|
|
|
|
} else {
|
|
|
|
key = `${path.replace(/^\//, '').replace(/\/$/, '')}/${fileName}`
|
|
|
|
}
|
|
|
|
const result = await client.delete(key) as any
|
|
|
|
return result.res.status === 204
|
|
|
|
} catch (error) {
|
2023-04-26 05:01:55 -04:00
|
|
|
console.log(error)
|
2023-02-15 10:36:47 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|