txOssSts.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * 微信小程序通过STS直传阿里云OSS
  3. *(uniapp版上传,小程序原生需要将uni.XXX替换为wx.XXX)
  4. *
  5. * @param {filePath} 图片临时地址
  6. * @param {option|Object} OSS和STS配置
  7. * @param {savePath} oss中的文件目录
  8. * @param {AccessKeySecret} 服务端返回的STS中的AccessKeySecret
  9. * @param {SecurityToken} 服务端返回的STS中的SecurityToken
  10. * @param {AccessKeyId} 服务端返回的STS中的AccessKeyId
  11. * @param {bucket} 存储桶
  12. * @param {area} 地区
  13. * @return {string|boolean} 成功返回文件地址,失败返回false
  14. */
  15. import COS from "cos-js-sdk-v5"
  16. import tools from "./tools";
  17. import {getTxySts} from "@/api/common";
  18. // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
  19. let Bucket = 'icoco-1317650740';
  20. // 存储桶Region可以在COS控制台指定存储桶的概览页查看 https://console.cloud.tencent.com/cos5/bucket/
  21. // 关于地域的详情见 https://cloud.tencent.com/document/product/436/6224
  22. let Region = 'ap-guangzhou';
  23. let cos=null;
  24. function startCos(){
  25. console.log('startCos-----------------')
  26. cos = new COS({
  27. getAuthorization: function (options, callback) {
  28. console.log('---------------获取初始化信息')
  29. let cosToken=tools.getCosToken()
  30. if(cosToken){
  31. callback({
  32. TmpSecretId: cosToken.credentials.tmpSecretId,
  33. TmpSecretKey: cosToken.credentials.tmpSecretKey,
  34. XCosSecurityToken: cosToken.credentials.sessionToken,
  35. StartTime: cosToken.startTime*1, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
  36. ExpiredTime: cosToken.expiredTime*1, // 时间戳,单位秒,如:1580000900
  37. })
  38. }else {
  39. getTxySts().then((res)=>{
  40. console.log('---------------获取初始化信息')
  41. if(res.code===0){
  42. tools.setCosToken(res.data)
  43. callback({
  44. TmpSecretId: res.data.credentials.tmpSecretId,
  45. TmpSecretKey: res.data.credentials.tmpSecretKey,
  46. XCosSecurityToken: res.data.credentials.sessionToken,
  47. StartTime: res.data.startTime*1, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
  48. ExpiredTime: res.data.expiredTime*1, // 时间戳,单位秒,如:1580000900
  49. })
  50. setTimeout(()=>{
  51. cos=null
  52. tools.delCosToken()
  53. },(res.data.expiredTime-res.data.startTime))
  54. }
  55. })
  56. }
  57. }
  58. });
  59. }
  60. let cosServe={}
  61. /**
  62. * COS文件上传
  63. * @param file
  64. * @param folder 上传目录
  65. * @returns {Promise<unknown>}
  66. */
  67. cosServe.txUploadFile=function (file,folder){
  68. if(cos===null){
  69. console.log('对象初始化')
  70. //初始化对象
  71. startCos();
  72. }
  73. //默认为个人目录gallery
  74. folder=folder==='moment'?'moment':'gallery'
  75. tools.showLoading()
  76. return new Promise((resolve, reject) => {
  77. console.log('cos-*-------------------------',cos)
  78. console.log(file)
  79. console.log(folder+'/'+tools.getDate()+tools.getRandFileName(file.name))
  80. // 分片上传文件
  81. cos.putObject({
  82. Bucket: Bucket,
  83. Region: Region,
  84. Key: folder+'/'+tools.getDate()+'/'+tools.getRandFileName(file.name),
  85. Body: file,
  86. onHashProgress: function (progressData) {
  87. console.log('校验中', JSON.stringify(progressData));
  88. },
  89. onProgress: function (progressData) {
  90. console.log('上传中', JSON.stringify(progressData));
  91. },
  92. }, function (err, data) {
  93. tools.hideLoading()
  94. console.log(err);
  95. console.log(data);
  96. if(data.Location!==undefined){
  97. data.Location='https://'+data.Location;
  98. resolve( data)
  99. }else {
  100. resolve( false)
  101. }
  102. });
  103. })
  104. }
  105. cosServe.getSignUrl= function (url){
  106. if(!url){
  107. return ''
  108. }
  109. if(cos===null){
  110. startCos();
  111. }
  112. console.log(url)
  113. url= url.replace('https://','')
  114. url= url.replace('http://','')
  115. url= url.replace('icoco-1317650740.cos.ap-guangzhou.myqcloud.com/','')
  116. console.log(url)
  117. console.log('开始签名----------------------------'+url)
  118. console.log({
  119. Bucket: Bucket,
  120. Region: Region,
  121. Key: url, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),支持中文,必须字段 */
  122. })
  123. return new Promise((resolve, reject) => {
  124. cos.getObjectUrl({
  125. Bucket: Bucket,
  126. Region: Region,
  127. Key: url, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),支持中文,必须字段 */
  128. },
  129. function (err, data) {
  130. console.log(err || data.Url);
  131. if(err){
  132. resolve('')
  133. }else {
  134. resolve(data.Url)
  135. }
  136. })
  137. })
  138. }
  139. export default cosServe