-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
254 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/CloudStriver/cloudmind-sts/biz/infrastructure/config" | ||
"github.com/CloudStriver/cloudmind-sts/biz/infrastructure/util/sdk/cos" | ||
gensts "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/sts" | ||
"github.com/google/wire" | ||
cossts "github.com/tencentyun/qcloud-cos-sts-sdk/go" | ||
"github.com/xh-polaris/platform-sts/biz/infrastructure/consts" | ||
"time" | ||
) | ||
|
||
type ICosService interface { | ||
GenCosSts(ctx context.Context, req *gensts.GenCosStsReq) (*gensts.GenCosStsResp, error) | ||
GenSignedUrl(ctx context.Context, req *gensts.GenSignedUrlReq) (*gensts.GenSignedUrlResp, error) | ||
DeleteObject(ctx context.Context, req *gensts.DeleteObjectReq) (*gensts.DeleteObjectResp, error) | ||
} | ||
|
||
type CosService struct { | ||
Config *config.Config | ||
CosSDK *cos.CosSDK | ||
} | ||
|
||
var CosSet = wire.NewSet( | ||
wire.Struct(new(CosService), "*"), | ||
wire.Bind(new(ICosService), new(*CosService)), | ||
) | ||
|
||
func (s *CosService) GenCosSts(ctx context.Context, req *gensts.GenCosStsReq) (*gensts.GenCosStsResp, error) { | ||
cosConfig := s.Config.CosConfig | ||
stsOption := &cossts.CredentialOptions{ | ||
// 临时密钥有效时长,单位是秒 | ||
DurationSeconds: int64(10 * time.Minute.Seconds()), | ||
Region: cosConfig.Region, | ||
Policy: &cossts.CredentialPolicy{ | ||
Statement: []cossts.CredentialPolicyStatement{ | ||
{ | ||
// 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923 | ||
Action: []string{ | ||
// 简单上传 | ||
"name/cos:PostObject", | ||
"name/cos:PutObject", | ||
// 分片上传 | ||
"name/cos:InitiateMultipartUpload", | ||
"name/cos:ListMultipartUploads", | ||
"name/cos:ListParts", | ||
"name/cos:UploadPart", | ||
"name/cos:CompleteMultipartUpload", | ||
}, | ||
Effect: "allow", | ||
// 密钥可控制的资源列表。此处开放名字为用户ID的文件夹及其子文件夹 | ||
Resource: []string{ | ||
fmt.Sprintf("qcs::cos:%s:uid/%s:%s/%s", | ||
cosConfig.Region, cosConfig.AppId, cosConfig.BucketName, req.Path), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
res, err := s.CosSDK.GetCredential(ctx, stsOption) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &gensts.GenCosStsResp{ | ||
SecretId: res.Credentials.TmpSecretID, | ||
SecretKey: res.Credentials.TmpSecretKey, | ||
SessionToken: res.Credentials.SessionToken, | ||
ExpiredTime: int64(res.ExpiredTime), | ||
StartTime: int64(res.StartTime), | ||
}, nil | ||
} | ||
|
||
func (s *CosService) GenSignedUrl(ctx context.Context, req *gensts.GenSignedUrlReq) (*gensts.GenSignedUrlResp, error) { | ||
signedUrl, err := s.CosSDK.GetPresignedURL(ctx, req.Method, req.Path, req.SecretId, req.SecretKey, time.Minute, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &gensts.GenSignedUrlResp{SignedUrl: signedUrl.String()}, nil | ||
} | ||
|
||
func (s *CosService) DeleteObject(ctx context.Context, req *gensts.DeleteObjectReq) (*gensts.DeleteObjectResp, error) { | ||
res, err := s.CosSDK.Delete(ctx, req.Path) | ||
if err != nil || res.StatusCode != 200 { | ||
return nil, consts.ErrCannotDeleteObject | ||
} | ||
return &gensts.DeleteObjectResp{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package cos | ||
|
||
import ( | ||
"context" | ||
"github.com/CloudStriver/cloudmind-sts/biz/infrastructure/config" | ||
"github.com/tencentyun/cos-go-sdk-v5" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/google/wire" | ||
sts "github.com/tencentyun/qcloud-cos-sts-sdk/go" | ||
"github.com/zeromicro/go-zero/core/trace" | ||
oteltrace "go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
type CosSDK struct { | ||
stsClient *sts.Client | ||
cosClient *cos.Client | ||
} | ||
|
||
func NewCosSDK(config *config.Config) (*CosSDK, error) { | ||
bucketURL, err := url.Parse(config.CosConfig.CosHost()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ciURL, err := url.Parse(config.CosConfig.CIHost()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &CosSDK{ | ||
stsClient: sts.NewClient( | ||
config.CosConfig.SecretId, | ||
config.CosConfig.SecretKey, | ||
nil), | ||
cosClient: cos.NewClient(&cos.BaseURL{ | ||
BucketURL: bucketURL, | ||
CIURL: ciURL, | ||
}, &http.Client{ | ||
Transport: &cos.AuthorizationTransport{ | ||
SecretID: config.CosConfig.SecretId, | ||
SecretKey: config.CosConfig.SecretKey, | ||
}, | ||
}), | ||
}, nil | ||
} | ||
|
||
func (s *CosSDK) GetCredential(ctx context.Context, opt *sts.CredentialOptions) (*sts.CredentialResult, error) { | ||
_, span := trace.TracerFromContext(ctx).Start(ctx, "sts/GetCredential", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient)) | ||
defer func() { | ||
span.End(oteltrace.WithTimestamp(time.Now())) | ||
}() | ||
return s.stsClient.GetCredential(opt) | ||
} | ||
|
||
func (s *CosSDK) GetPresignedURL(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt interface{}, signHost ...bool) (*url.URL, error) { | ||
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "cos/Object/GetPresignedURL", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient)) | ||
defer func() { | ||
span.End(oteltrace.WithTimestamp(time.Now())) | ||
}() | ||
return s.cosClient.Object.GetPresignedURL(ctx, httpMethod, name, ak, sk, expired, opt, signHost...) | ||
} | ||
|
||
func (s *CosSDK) Delete(ctx context.Context, name string, opt ...*cos.ObjectDeleteOptions) (*cos.Response, error) { | ||
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "cos/Object/Delete", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient)) | ||
defer func() { | ||
span.End(oteltrace.WithTimestamp(time.Now())) | ||
}() | ||
return s.cosClient.Object.Delete(ctx, name, opt...) | ||
} | ||
|
||
func (s *CosSDK) BatchImageAuditing(ctx context.Context, opt *cos.BatchImageAuditingOptions) (*cos.BatchImageAuditingJobResult, *cos.Response, error) { | ||
ctx, span := trace.TracerFromContext(ctx).Start(ctx, "cos/CI/BatchImageAuditing", oteltrace.WithTimestamp(time.Now()), oteltrace.WithSpanKind(oteltrace.SpanKindClient)) | ||
defer func() { | ||
span.End(oteltrace.WithTimestamp(time.Now())) | ||
}() | ||
return s.cosClient.CI.BatchImageAuditing(ctx, opt) | ||
} | ||
|
||
var CosSet = wire.NewSet( | ||
NewCosSDK, | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.