cos_temp_key.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package domainservice
  2. import (
  3. "Cold_Logistic/internal/pkg/common/global"
  4. "Cold_Logistic/internal/pkg/common/options"
  5. sts "github.com/tencentyun/qcloud-cos-sts-sdk/go"
  6. myCos "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/objectStore/cos"
  7. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  8. )
  9. // GetCosTempKey 获取临时秘钥
  10. func GetCosTempKey(keyPath string) (res *global.Credentials, err error) {
  11. storage := options.OptInstance.Storage
  12. cosStore, err := myCos.NewCosObjectStore(storage.CosStorageOptions)
  13. if err != nil {
  14. return res, errors.WithStackOnce(err)
  15. }
  16. credentials, err := cosStore.GetTmpCredential(keyPath)
  17. if err != nil {
  18. return res, errors.WithStackOnce(err)
  19. }
  20. res = &global.Credentials{}
  21. res.BaseUrl = storage.CosStorageOptions.BaseURL
  22. res.Bucket = storage.CosStorageOptions.Bucket
  23. res.Region = storage.CosStorageOptions.Region
  24. res.StartTime = credentials.StartTime
  25. res.ExpiredTime = credentials.ExpiredTime
  26. res.TmpSecretKey = credentials.Credentials.TmpSecretKey
  27. res.TmpSecretID = credentials.Credentials.TmpSecretID
  28. res.SessionToken = credentials.Credentials.SessionToken
  29. return res, nil
  30. }
  31. func GetCosTempManyKey(keyPaths []string) (res *global.Credentials, err error) {
  32. storage := options.OptInstance.Storage
  33. cosStore, err := myCos.NewCosObjectStore(storage.CosStorageOptions)
  34. if err != nil {
  35. return res, errors.WithStackOnce(err)
  36. }
  37. credentials, err := cosStore.GetManyTmpCredentialForDirKeyPath(keyPaths)
  38. if err != nil {
  39. return res, errors.WithStackOnce(err)
  40. }
  41. res = &global.Credentials{}
  42. res.BaseUrl = storage.CosStorageOptions.BaseURL
  43. res.Bucket = storage.CosStorageOptions.Bucket
  44. res.Region = storage.CosStorageOptions.Region
  45. res.StartTime = credentials.StartTime
  46. res.ExpiredTime = credentials.ExpiredTime
  47. res.TmpSecretKey = credentials.Credentials.TmpSecretKey
  48. res.TmpSecretID = credentials.Credentials.TmpSecretID
  49. res.SessionToken = credentials.Credentials.SessionToken
  50. return res, nil
  51. }
  52. func GetCosPathTempKey(keyPath string, durationSecond int64) (res *global.Credentials, err error) {
  53. storage := options.OptInstance.Storage
  54. cosStore, err := myCos.NewCosObjectStore(storage.CosStorageOptions)
  55. if err != nil {
  56. return res, errors.WithStackOnce(err)
  57. }
  58. credentials, err := cosStore.GetTmpCredentialForDirKeyPath(keyPath, func(credentialOptions *sts.CredentialOptions) {
  59. if durationSecond > 0 {
  60. credentialOptions.DurationSeconds = durationSecond
  61. }
  62. })
  63. if err != nil {
  64. return res, errors.WithStackOnce(err)
  65. }
  66. res = &global.Credentials{}
  67. res.BaseUrl = storage.CosStorageOptions.BaseURL
  68. res.Bucket = storage.CosStorageOptions.Bucket
  69. res.Region = storage.CosStorageOptions.Region
  70. res.StartTime = credentials.StartTime
  71. res.ExpiredTime = credentials.ExpiredTime
  72. res.TmpSecretKey = credentials.Credentials.TmpSecretKey
  73. res.TmpSecretID = credentials.Credentials.TmpSecretID
  74. res.SessionToken = credentials.Credentials.SessionToken
  75. return res, nil
  76. }