package domainservice import ( "Cold_Logistic/internal/pkg/common/global" "Cold_Logistic/internal/pkg/common/options" sts "github.com/tencentyun/qcloud-cos-sts-sdk/go" myCos "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/objectStore/cos" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors" ) // GetCosTempKey 获取临时秘钥 func GetCosTempKey(keyPath string) (res *global.Credentials, err error) { storage := options.OptInstance.Storage cosStore, err := myCos.NewCosObjectStore(storage.CosStorageOptions) if err != nil { return res, errors.WithStackOnce(err) } credentials, err := cosStore.GetTmpCredential(keyPath) if err != nil { return res, errors.WithStackOnce(err) } res = &global.Credentials{} res.BaseUrl = storage.CosStorageOptions.BaseURL res.Bucket = storage.CosStorageOptions.Bucket res.Region = storage.CosStorageOptions.Region res.StartTime = credentials.StartTime res.ExpiredTime = credentials.ExpiredTime res.TmpSecretKey = credentials.Credentials.TmpSecretKey res.TmpSecretID = credentials.Credentials.TmpSecretID res.SessionToken = credentials.Credentials.SessionToken return res, nil } func GetCosTempManyKey(keyPaths []string) (res *global.Credentials, err error) { storage := options.OptInstance.Storage cosStore, err := myCos.NewCosObjectStore(storage.CosStorageOptions) if err != nil { return res, errors.WithStackOnce(err) } credentials, err := cosStore.GetManyTmpCredentialForDirKeyPath(keyPaths) if err != nil { return res, errors.WithStackOnce(err) } res = &global.Credentials{} res.BaseUrl = storage.CosStorageOptions.BaseURL res.Bucket = storage.CosStorageOptions.Bucket res.Region = storage.CosStorageOptions.Region res.StartTime = credentials.StartTime res.ExpiredTime = credentials.ExpiredTime res.TmpSecretKey = credentials.Credentials.TmpSecretKey res.TmpSecretID = credentials.Credentials.TmpSecretID res.SessionToken = credentials.Credentials.SessionToken return res, nil } func GetCosPathTempKey(keyPath string, durationSecond int64) (res *global.Credentials, err error) { storage := options.OptInstance.Storage cosStore, err := myCos.NewCosObjectStore(storage.CosStorageOptions) if err != nil { return res, errors.WithStackOnce(err) } credentials, err := cosStore.GetTmpCredentialForDirKeyPath(keyPath, func(credentialOptions *sts.CredentialOptions) { if durationSecond > 0 { credentialOptions.DurationSeconds = durationSecond } }) if err != nil { return res, errors.WithStackOnce(err) } res = &global.Credentials{} res.BaseUrl = storage.CosStorageOptions.BaseURL res.Bucket = storage.CosStorageOptions.Bucket res.Region = storage.CosStorageOptions.Region res.StartTime = credentials.StartTime res.ExpiredTime = credentials.ExpiredTime res.TmpSecretKey = credentials.Credentials.TmpSecretKey res.TmpSecretID = credentials.Credentials.TmpSecretID res.SessionToken = credentials.Credentials.SessionToken return res, nil }