txOssSts.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 getFileAccessUrl from "@/js_sdk/tencentcloud-plugin-cos/get-file-access-url";
  16. import tools from "./tools";
  17. import uploadFile from "@/js_sdk/tencentcloud-plugin-cos/upload-file";
  18. let cosServe={}
  19. /**
  20. * COS文件上传
  21. * @param file
  22. * @param folder 上传目录
  23. * @returns {Promise<unknown>}
  24. */
  25. cosServe.txUploadFile=async function (file,folder){
  26. //默认为个人目录gallery
  27. tools.showLoading()
  28. folder=folder==='moment'?'moment':'gallery'
  29. let user_id= uni.getStorageSync('user_id')
  30. if(!user_id){
  31. user_id='00'
  32. }
  33. if(!file.name){
  34. file.name=file.path
  35. }
  36. let key=folder+'/'+user_id+'/'+tools.getDate()+'/'+tools.getRandFileName(file.name)
  37. // #ifdef APP-PLUS
  38. file =file.path;
  39. //#endif
  40. let url = await uploadFile(file,key);
  41. tools.hideLoading()
  42. return url
  43. }
  44. cosServe.getSignUrl= async function (url){
  45. if(!url){
  46. return ''
  47. }
  48. console.log(url)
  49. url= url.replace('https://','')
  50. url= url.replace('http://','')
  51. url= url.replace('icoco-1317650740.cos.ap-guangzhou.myqcloud.com/','')
  52. let signUrl = await getFileAccessUrl(url);
  53. console.log(signUrl)
  54. return signUrl
  55. }
  56. export default cosServe