feat: chore oss upload
This commit is contained in:
@@ -3,6 +3,7 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lone-services/pkg/utils"
|
||||
chore "lone-services/rpc/chore/pb"
|
||||
@@ -21,12 +22,12 @@ type PolicyLogic struct {
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
// StsToken 对齐阿里云 STS 返回字段,并附带前端上传所需信息
|
||||
// StsToken 对齐阿里云 STS Credentials,并附带前端上传所需信息
|
||||
type StsToken struct {
|
||||
AccessKeyId string `json:"AccessKeyId"`
|
||||
AccessKeySecret string `json:"AccessKeySecret"`
|
||||
SecurityToken string `json:"SecurityToken"`
|
||||
Expiration string `json:"Expiration"`
|
||||
AccessKeyId string `json:"access_key_id"`
|
||||
AccessKeySecret string `json:"access_key_secret"`
|
||||
SecurityToken string `json:"security_token"`
|
||||
Expiration string `json:"expiration"`
|
||||
Bucket string `json:"bucket"`
|
||||
Region string `json:"region"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
@@ -34,6 +35,17 @@ type StsToken struct {
|
||||
Dir string `json:"dir"`
|
||||
}
|
||||
|
||||
func formatExpiration(raw string) string {
|
||||
if raw == "" {
|
||||
return raw
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339, raw)
|
||||
if err != nil {
|
||||
return raw
|
||||
}
|
||||
return t.Local().Format(utils.YMDHIS)
|
||||
}
|
||||
|
||||
func NewPolicyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PolicyLogic {
|
||||
return &PolicyLogic{
|
||||
ctx: ctx,
|
||||
@@ -42,6 +54,24 @@ func NewPolicyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PolicyLogi
|
||||
}
|
||||
}
|
||||
|
||||
func CreateClient(accessKeyId, accessKeySecret, endpoint *string) (*sts20150401.Client, error) {
|
||||
config := &openapi.Config{
|
||||
AccessKeyId: accessKeyId,
|
||||
AccessKeySecret: accessKeySecret,
|
||||
}
|
||||
config.Endpoint = endpoint
|
||||
return sts20150401.NewClient(config)
|
||||
}
|
||||
|
||||
func AssumeRole(client *sts20150401.Client, roleArn, roleSessionName string, durationSeconds int64) (*sts20150401.AssumeRoleResponse, error) {
|
||||
assumeRoleRequest := &sts20150401.AssumeRoleRequest{
|
||||
DurationSeconds: tea.Int64(durationSeconds),
|
||||
RoleArn: tea.String(roleArn),
|
||||
RoleSessionName: tea.String(roleSessionName),
|
||||
}
|
||||
return client.AssumeRoleWithOptions(assumeRoleRequest, &util.RuntimeOptions{})
|
||||
}
|
||||
|
||||
func (l *PolicyLogic) Policy(in *chore.PolicyReq) (*chore.Response, error) {
|
||||
accessKeyId := utils.GetConfigString("oss.accessKeyId")
|
||||
accessKeySecret := utils.GetConfigString("oss.accessKeySecret")
|
||||
@@ -88,18 +118,13 @@ func (l *PolicyLogic) Policy(in *chore.PolicyReq) (*chore.Response, error) {
|
||||
expire = 3600
|
||||
}
|
||||
|
||||
client, err := createStsClient(accessKeyId, accessKeySecret, stsEndpoint)
|
||||
client, err := CreateClient(tea.String(accessKeyId), tea.String(accessKeySecret), tea.String(stsEndpoint))
|
||||
if err != nil {
|
||||
l.Errorf("create STS client: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
}
|
||||
|
||||
assumeRoleRequest := &sts20150401.AssumeRoleRequest{
|
||||
DurationSeconds: tea.Int64(expire),
|
||||
RoleArn: tea.String(roleArn),
|
||||
RoleSessionName: tea.String(roleSessionName),
|
||||
}
|
||||
resp, err := client.AssumeRoleWithOptions(assumeRoleRequest, &util.RuntimeOptions{})
|
||||
resp, err := AssumeRole(client, roleArn, roleSessionName, expire)
|
||||
if err != nil {
|
||||
l.Errorf("AssumeRole: %v", err)
|
||||
return failResponse(utils.Fail), nil
|
||||
@@ -114,7 +139,7 @@ func (l *PolicyLogic) Policy(in *chore.PolicyReq) (*chore.Response, error) {
|
||||
AccessKeyId: tea.StringValue(cred.AccessKeyId),
|
||||
AccessKeySecret: tea.StringValue(cred.AccessKeySecret),
|
||||
SecurityToken: tea.StringValue(cred.SecurityToken),
|
||||
Expiration: tea.StringValue(cred.Expiration),
|
||||
Expiration: formatExpiration(tea.StringValue(cred.Expiration)),
|
||||
Bucket: bucket,
|
||||
Region: region,
|
||||
Endpoint: endpoint,
|
||||
@@ -123,12 +148,3 @@ func (l *PolicyLogic) Policy(in *chore.PolicyReq) (*chore.Response, error) {
|
||||
}
|
||||
return okResponse(token), nil
|
||||
}
|
||||
|
||||
func createStsClient(accessKeyId, accessKeySecret, endpoint string) (*sts20150401.Client, error) {
|
||||
config := &openapi.Config{
|
||||
AccessKeyId: tea.String(accessKeyId),
|
||||
AccessKeySecret: tea.String(accessKeySecret),
|
||||
Endpoint: tea.String(endpoint),
|
||||
}
|
||||
return sts20150401.NewClient(config)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user