diff --git a/package.json b/package.json index 8345d0e..cf63c8a 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "axios": "0.28.1", "clipboard": "2.0.8", "core-js": "^3.41.0", + "cos-js-sdk-v5": "^1.8.7", "echarts": "5.4.0", "element-ui": "2.15.14", "file-saver": "2.0.5", diff --git a/src/components/ImageUpload/index.vue b/src/components/ImageUpload/index.vue index 9596be4..f32c311 100644 --- a/src/components/ImageUpload/index.vue +++ b/src/components/ImageUpload/index.vue @@ -2,21 +2,18 @@
@@ -44,7 +41,7 @@ + - diff --git a/src/utils/cos.js b/src/utils/cos.js new file mode 100644 index 0000000..60d46fe --- /dev/null +++ b/src/utils/cos.js @@ -0,0 +1,40 @@ +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;