PicList/src/renderer/apis/aliyun.ts
2024-04-15 23:12:02 +08:00

28 lines
728 B
TypeScript

import OSS from 'ali-oss'
interface IConfigMap {
fileName: string
config: PartialKeys<IAliYunConfig, 'path'>
}
export default class AliyunApi {
static #getKey (fileName: string, path?: string): string {
return path && path !== '/'
? `${path.replace(/^\/+|\/+$/, '')}/${fileName}`
: fileName
}
static async delete (configMap: IConfigMap): Promise<boolean> {
const { fileName, config } = configMap
try {
const client = new OSS({ ...config, region: config.area })
const key = AliyunApi.#getKey(fileName, config.path)
const result = await client.delete(key)
return result.res.status === 204
} catch (error) {
console.error(error)
return false
}
}
}