|
|
@@ -12,71 +12,12 @@
|
|
|
* @param {area} 地区
|
|
|
* @return {string|boolean} 成功返回文件地址,失败返回false
|
|
|
*/
|
|
|
-// #ifdef H5
|
|
|
-
|
|
|
-//#endif
|
|
|
-// #ifdef APP-PLUS
|
|
|
-
|
|
|
-//#endif
|
|
|
|
|
|
|
|
|
import getFileAccessUrl from "@/js_sdk/tencentcloud-plugin-cos/get-file-access-url";
|
|
|
-
|
|
|
-console.log('COS加载js版本')
|
|
|
-import COS from "cos-js-sdk-v5"
|
|
|
-// import COS from "./cos-wx-sdk-v5.min"
|
|
|
-// console.log('COS加载node版本')
|
|
|
-// import COS from "cos-nodejs-sdk-v5"
|
|
|
import tools from "./tools";
|
|
|
-import {getTxySts} from "@/api/common";
|
|
|
-import {secretId, secretKey} from "@/uniCloud-aliyun/cloudfunctions/tencentcloud-plugin/config";
|
|
|
-
|
|
|
+import uploadFile from "@/js_sdk/tencentcloud-plugin-cos/upload-file";
|
|
|
|
|
|
-
|
|
|
-// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
|
|
|
-let Bucket = 'icoco-1317650740';
|
|
|
-// 存储桶Region可以在COS控制台指定存储桶的概览页查看 https://console.cloud.tencent.com/cos5/bucket/
|
|
|
-// 关于地域的详情见 https://cloud.tencent.com/document/product/436/6224
|
|
|
-let Region = 'ap-guangzhou';
|
|
|
-
|
|
|
-let cos=null;
|
|
|
-
|
|
|
-/**
|
|
|
- * 获取sts权限信息
|
|
|
- * @returns {Promise<*>}
|
|
|
- */
|
|
|
- async function getSts(){
|
|
|
- let cosToken=tools.getCosToken()
|
|
|
- if(!cosToken) {
|
|
|
- let res =await getTxySts()
|
|
|
- if(res.code===0){
|
|
|
- tools.setCosToken(res.data)
|
|
|
- setTimeout(()=>{
|
|
|
- cos=null
|
|
|
- tools.delCosToken()
|
|
|
- },(res.data.expiredTime-res.data.startTime))
|
|
|
- }
|
|
|
- }
|
|
|
- return cosToken
|
|
|
-}
|
|
|
-
|
|
|
-function startCos(){
|
|
|
- console.log('startCos-----------------')
|
|
|
- cos = new COS({
|
|
|
- getAuthorization:async function (options, callback) {
|
|
|
- console.log('---------------获取初始化信息')
|
|
|
- let cosToken= await getSts()
|
|
|
- console.log(cosToken)
|
|
|
- callback({
|
|
|
- TmpSecretId: cosToken.credentials.tmpSecretId,
|
|
|
- TmpSecretKey: cosToken.credentials.tmpSecretKey,
|
|
|
- XCosSecurityToken: cosToken.credentials.sessionToken,
|
|
|
- StartTime: cosToken.startTime*1, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
|
|
|
- ExpiredTime: cosToken.expiredTime*1, // 时间戳,单位秒,如:1580000900
|
|
|
- })
|
|
|
- }
|
|
|
- });
|
|
|
-}
|
|
|
let cosServe={}
|
|
|
/**
|
|
|
* COS文件上传
|
|
|
@@ -84,97 +25,31 @@ let cosServe={}
|
|
|
* @param folder 上传目录
|
|
|
* @returns {Promise<unknown>}
|
|
|
*/
|
|
|
-cosServe.txUploadFile=function (file,folder){
|
|
|
- if(cos===null){
|
|
|
- console.log('对象初始化')
|
|
|
- //初始化对象
|
|
|
- startCos();
|
|
|
- }
|
|
|
+cosServe.txUploadFile=async function (file,folder){
|
|
|
+
|
|
|
//默认为个人目录gallery
|
|
|
folder=folder==='moment'?'moment':'gallery'
|
|
|
- // tools.showLoading()
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- console.log('cos-*-------------------------',cos)
|
|
|
- console.log(file)
|
|
|
- console.log(folder+'/'+tools.getDate()+tools.getRandFileName(file.name))
|
|
|
- // 分片上传文件
|
|
|
- cos.putObject({
|
|
|
- Bucket: Bucket,
|
|
|
- Region: Region,
|
|
|
- Key: folder+'/'+tools.getDate()+'/'+tools.getRandFileName(file.name),
|
|
|
- Body: file,
|
|
|
- onHashProgress: function (progressData) {
|
|
|
- console.log('校验中', JSON.stringify(progressData));
|
|
|
- },
|
|
|
- onProgress: function (progressData) {
|
|
|
- console.log('上传中', JSON.stringify(progressData));
|
|
|
- },
|
|
|
- }, function (err, data) {
|
|
|
- tools.hideLoading()
|
|
|
- console.log(err);
|
|
|
- console.log(data);
|
|
|
- if(data.Location!==undefined){
|
|
|
- data.Location='https://'+data.Location;
|
|
|
- resolve( data)
|
|
|
- }else {
|
|
|
- resolve( false)
|
|
|
- }
|
|
|
+ let key=folder+'/'+tools.getDate()+'/'+tools.getRandFileName(file.name)
|
|
|
+ let url = await uploadFile(file,key);
|
|
|
+ console.log(url)
|
|
|
+ return url
|
|
|
+}
|
|
|
|
|
|
- });
|
|
|
- })
|
|
|
|
|
|
-}
|
|
|
|
|
|
-cosServe.getSignObject=async function (url){
|
|
|
+cosServe.getSignUrl= async function (url){
|
|
|
if(!url){
|
|
|
return ''
|
|
|
}
|
|
|
-
|
|
|
console.log(url)
|
|
|
url= url.replace('https://','')
|
|
|
url= url.replace('http://','')
|
|
|
url= url.replace('icoco-1317650740.cos.ap-guangzhou.myqcloud.com/','')
|
|
|
- let signUrl = await getFileAccessUrl(url);
|
|
|
+ let signUrl = await getFileAccessUrl(url);
|
|
|
console.log(signUrl)
|
|
|
return signUrl
|
|
|
|
|
|
}
|
|
|
|
|
|
-cosServe.getSignUrl= function (url){
|
|
|
- if(!url){
|
|
|
- return ''
|
|
|
- }
|
|
|
- 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)
|
|
|
- console.log({
|
|
|
- Bucket: Bucket,
|
|
|
- Region: Region,
|
|
|
- Key: url, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),支持中文,必须字段 */
|
|
|
- })
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- cos.getObjectUrl({
|
|
|
- Bucket: Bucket,
|
|
|
- Region: Region,
|
|
|
- Key: url, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),支持中文,必须字段 */
|
|
|
- },
|
|
|
- function (err, data) {
|
|
|
- console.log(err || data.Url);
|
|
|
- if(err){
|
|
|
- resolve('')
|
|
|
- }else {
|
|
|
- resolve(data.Url)
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
export default cosServe
|