|
|
@@ -13,11 +13,11 @@
|
|
|
* @return {string|boolean} 成功返回文件地址,失败返回false
|
|
|
*/
|
|
|
import COS from "cos-js-sdk-v5"
|
|
|
-
|
|
|
-console.log(COS)
|
|
|
import tools from "./tools";
|
|
|
import {getTxySts} from "@/api/common";
|
|
|
|
|
|
+
|
|
|
+
|
|
|
// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
|
|
|
let Bucket = 'icoco-1317650740';
|
|
|
// 存储桶Region可以在COS控制台指定存储桶的概览页查看 https://console.cloud.tencent.com/cos5/bucket/
|
|
|
@@ -30,38 +30,48 @@ function startCos(){
|
|
|
cos = new COS({
|
|
|
getAuthorization: function (options, callback) {
|
|
|
console.log('---------------获取初始化信息')
|
|
|
+ let cosToken=tools.getCosToken()
|
|
|
+ if(cosToken){
|
|
|
+ callback({
|
|
|
+ TmpSecretId: cosToken.credentials.tmpSecretId,
|
|
|
+ TmpSecretKey: cosToken.credentials.tmpSecretKey,
|
|
|
+ XCosSecurityToken: cosToken.credentials.sessionToken,
|
|
|
+ StartTime: cosToken.startTime*1, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
|
|
|
+ ExpiredTime: cosToken.expiredTime*1, // 时间戳,单位秒,如:1580000900
|
|
|
+ })
|
|
|
+ }else {
|
|
|
+ getTxySts().then((res)=>{
|
|
|
|
|
|
- getTxySts().then((res)=>{
|
|
|
- console.log({
|
|
|
- TmpSecretId: res.data.credentials.tmpSecretId,
|
|
|
- TmpSecretKey: res.data.credentials.tmpSecretKey,
|
|
|
- XCosSecurityToken: res.data.credentials.sessionToken,
|
|
|
- StartTime: res.data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
|
|
|
- ExpiredTime: res.data.expiredTime, // 时间戳,单位秒,如:1580000900
|
|
|
+ console.log('---------------获取初始化信息')
|
|
|
+ if(res.code===0){
|
|
|
+ tools.setCosToken(res.data)
|
|
|
+ callback({
|
|
|
+ TmpSecretId: res.data.credentials.tmpSecretId,
|
|
|
+ TmpSecretKey: res.data.credentials.tmpSecretKey,
|
|
|
+ XCosSecurityToken: res.data.credentials.sessionToken,
|
|
|
+ StartTime: res.data.startTime*1, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
|
|
|
+ ExpiredTime: res.data.expiredTime*1, // 时间戳,单位秒,如:1580000900
|
|
|
+ })
|
|
|
+ setTimeout(()=>{
|
|
|
+ cos=null
|
|
|
+ tools.delCosToken()
|
|
|
+ },(res.data.expiredTime-res.data.startTime))
|
|
|
+ }
|
|
|
})
|
|
|
- console.log('---------------获取初始化信息')
|
|
|
- if(res.code===0){
|
|
|
- callback({
|
|
|
- TmpSecretId: res.data.credentials.tmpSecretId,
|
|
|
- TmpSecretKey: res.data.credentials.tmpSecretKey,
|
|
|
- XCosSecurityToken: res.data.credentials.sessionToken,
|
|
|
- StartTime: res.data.startTime*1, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
|
|
|
- ExpiredTime: res.data.expiredTime*1, // 时间戳,单位秒,如:1580000900
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
+let cosServe={}
|
|
|
/**
|
|
|
* COS文件上传
|
|
|
* @param file
|
|
|
* @param folder 上传目录
|
|
|
* @returns {Promise<unknown>}
|
|
|
*/
|
|
|
-function txUploadFile(file,folder){
|
|
|
+cosServe.txUploadFile=function (file,folder){
|
|
|
if(cos===null){
|
|
|
console.log('对象初始化')
|
|
|
//初始化对象
|
|
|
@@ -103,5 +113,26 @@ function txUploadFile(file,folder){
|
|
|
}
|
|
|
|
|
|
|
|
|
+cosServe.getSignUrl=function (url){
|
|
|
+ if(cos===null){
|
|
|
+ startCos();
|
|
|
+ }
|
|
|
+ console.log(url)
|
|
|
+ url= url.replace('https://','')
|
|
|
+ url= url.replace('http://','')
|
|
|
+ url= url.replace('icoco-1317650740.cos.ap-guangzhou.myqcloud.com','')
|
|
|
+ console.log(url)
|
|
|
+ console.log('开始签名----------------------------'+url)
|
|
|
+ let newUrl= cos.getObjectUrl({
|
|
|
+ Bucket: Bucket,
|
|
|
+ Region: Region,
|
|
|
+ Key: url, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),支持中文,必须字段 */
|
|
|
+ Sign: true,
|
|
|
+ })
|
|
|
+ console.log('签名完成----------------------------'+newUrl)
|
|
|
+ console.log(newUrl)
|
|
|
+ return newUrl
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
-export default txUploadFile
|
|
|
+export default cosServe
|