123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /**
- * 微信小程序通过STS直传阿里云OSS
- *(uniapp版上传,小程序原生需要将uni.XXX替换为wx.XXX)
- *
- * @param {filePath} 图片临时地址
- * @param {option|Object} OSS和STS配置
- * @param {savePath} oss中的文件目录
- * @param {AccessKeySecret} 服务端返回的STS中的AccessKeySecret
- * @param {SecurityToken} 服务端返回的STS中的SecurityToken
- * @param {AccessKeyId} 服务端返回的STS中的AccessKeyId
- * @param {bucket} 存储桶
- * @param {area} 地区
- * @return {string|boolean} 成功返回文件地址,失败返回false
- */
- import COS from "cos-js-sdk-v5"
- 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/
- // 关于地域的详情见 https://cloud.tencent.com/document/product/436/6224
- let Region = 'ap-guangzhou';
- let cos=null;
- function startCos(){
- console.log('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)=>{
- 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))
- }
- })
- }
- }
- });
- }
- let cosServe={}
- /**
- * COS文件上传
- * @param file
- * @param folder 上传目录
- * @returns {Promise<unknown>}
- */
- cosServe.txUploadFile=function (file,folder){
- if(cos===null){
- console.log('对象初始化')
- //初始化对象
- startCos();
- }
- //默认为个人目录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)
- }
- });
- })
- }
- 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),支持中文,必须字段 */
- })
- let newUrl= cos.getObjectUrl({
- Bucket: Bucket,
- Region: Region,
- Key: url, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),支持中文,必须字段 */
- })
- console.log('签名完成----------------------------'+newUrl)
- console.log(newUrl)
- return newUrl
- }
- export default cosServe
|