NodeJs获取签名原创
1、安装cos的nodejs依赖
2、使用
const COS = require('cos-nodejs-sdk-v5')
const cos = new COS({
SecretId: config.secretId,
SecretKey: config.secretKey,
// 是否自定义域名
Domain: config.customUrl ? config.customUrl : ''
})
const key = (config.path ? config.path : '') + img.fileName
// 去掉问号
const queryStr = config.path?.startsWith('?') ? config.path.substring(1) : config.path
const query = new Map(queryStr.split('&').map(value => {
const arr = value.split('=')
return [arr[0], arr]
}))
let url = cos.getObjectUrl({
Bucket: config.bucket,
Region: config.area,
Key: key,
Sign: true,
Query: query,
Expires: expireSeconds
}, (err, data) => {
if (err) {
ctx.log.warn(err.message)
}
url = data.Url
})
return url
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
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
上次更新: 2025/02/18 14:46:10