41 lines
1.6 KiB
JavaScript
41 lines
1.6 KiB
JavaScript
import COS from 'cos-js-sdk-v5';
|
||
import request from '@/utils/request'; // 引入你的封装请求方法
|
||
const cos = new COS({
|
||
// 其他配置项可参考下方 初始化配置项
|
||
// getAuthorization 必选参数
|
||
getAuthorization: function (options, callback) {
|
||
// 初始化时不会调用,只有调用 cos 方法(例如 cos.putObject)时才会进入
|
||
// 异步获取临时密钥
|
||
// 服务端 JS 示例:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
|
||
// 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
|
||
// STS 详细文档指引看:https://cloud.tencent.com/document/product/436/14048
|
||
const stsUrl = '/cos/sts'; // 使用相对路径,避免本地开发跨域问题
|
||
request({
|
||
url: stsUrl,
|
||
method: 'GET'
|
||
}).then(response => {
|
||
console.log('执行了')
|
||
const data = response; // 适配你的 request 返回格式
|
||
if (!data || !data.credentials) {
|
||
return console.error('credentials invalid:\n' + JSON.stringify(data, null, 2));
|
||
}
|
||
|
||
const {credentials} = data;
|
||
|
||
callback({
|
||
TmpSecretId: credentials.tmpSecretId,
|
||
TmpSecretKey: credentials.tmpSecretKey,
|
||
SecurityToken: credentials.sessionToken,
|
||
StartTime: data.startTime, // 时间戳,单位秒
|
||
ExpiredTime: data.expiredTime, // 时间戳,单位秒
|
||
ScopeLimit: true // 细粒度控制权限需要设为 true
|
||
});
|
||
|
||
}).catch(error => {
|
||
console.error('获取临时密钥失败', error);
|
||
});
|
||
|
||
}
|
||
});
|
||
export default cos;
|