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)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
login_out_time=43200 #api接口超时时间分
|
||||
login_refresh_out_time=83200
|
||||
name = "chore-service"
|
||||
listenOn = "0.0.0.0:10500"
|
||||
listenOn = "0.0.0.0:10300"
|
||||
mode = "dev"
|
||||
[log]
|
||||
path = "logs"
|
||||
@@ -18,8 +18,8 @@
|
||||
[oss]
|
||||
accessKeyId = "LTAI5t7U8KvxSiPE5auwQUuu"
|
||||
accessKeySecret = "xjfgbQQRpL4TcIspNuClg6bYADa3pk"
|
||||
roleArn = "acs:ram::你的账号ID:role/你的角色名"
|
||||
roleSessionName = "chore-oss-upload"
|
||||
roleArn = "acs:ram::1663896742753915:role/oss-upload"
|
||||
roleSessionName = "oss-upload"
|
||||
region = "oss-cn-hangzhou"
|
||||
bucketName = "lone-images"
|
||||
endpoint = "https://lone-images.oss-accelerate.aliyuncs.com"
|
||||
@@ -34,4 +34,4 @@
|
||||
db = 0
|
||||
|
||||
[encrypt]
|
||||
data_key = "u2t9T3luZtoRfhBstkFN6TiIMW38BA8a"
|
||||
data_key = "u2t9T3luZtoRfhBstkFN6TiIMW38BA8a"
|
||||
@@ -89,6 +89,7 @@ type UserListRow struct {
|
||||
OnTrialNum int `gorm:"column:on_trial_num" json:"on_trial_num"`
|
||||
Gender int `gorm:"column:gender" json:"gender"`
|
||||
Birthday string `gorm:"column:birthday" json:"birthday"`
|
||||
Status uint8 `gorm:"column:status" json:"status"`
|
||||
CreatedAt utils.CustomTime `gorm:"column:created_at" json:"created_at"`
|
||||
UpdatedAt utils.CustomTime `gorm:"column:updated_at" json:"updated_at"`
|
||||
}
|
||||
@@ -102,6 +103,7 @@ type UserListItem struct {
|
||||
OnTrialNum int `json:"on_trial_num"`
|
||||
Gender int `json:"gender"`
|
||||
Birthday string `json:"birthday"`
|
||||
Status uint8 `json:"status"`
|
||||
CreatedAt utils.CustomTime `json:"created_at"`
|
||||
UpdatedAt utils.CustomTime `json:"updated_at"`
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ func (l *UserItemsLogic) UserItems(in *user.UserItemsReq) (*user.Response, error
|
||||
OnTrialNum: row.OnTrialNum,
|
||||
Gender: row.Gender,
|
||||
Birthday: row.Birthday,
|
||||
Status: row.Status,
|
||||
CreatedAt: row.CreatedAt,
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user