ThinkJS Service 删除阿里云OSS文件

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
'use strict';

/**
* 阿里云OSS
* Created on 2018-12-21
*/
const OSS = require('ali-oss');

const region = 'oss-cn-shenzhen';
// ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换
const accessKeyId = '****************';
const accessKeySecret = '******************************';
const bucket = '*****************';

module.exports = class extends think.Service {
/**
* @description 删除OSS文件
* @param {string} fileName 文件名
* @return {boolean} 文件不存在也返回true,请求错误返回false
*/
async deleteFile(fileName) {
try {
let client = new OSS({
region,
accessKeyId,
accessKeySecret,
bucket
});
let result = await client.delete(fileName);
let ret = result.res.status == 204;
return ret;
} catch (e) {
think.logger.info(e);
}
}
}

参考

作者

DullSword

发布于

2019-02-19

更新于

2024-11-05

许可协议

评论