feat: update ali oss client
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package ali
|
||||
|
||||
import (
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/opt"
|
||||
)
|
||||
@@ -32,25 +34,16 @@ func Provide(opts ...opt.Option) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return container.Container.Provide(func() (*Config, error) {
|
||||
return &config, nil
|
||||
return container.Container.Provide(func() (*OSSClient, error) {
|
||||
cred := credentials.NewStaticCredentialsProvider(config.AccessKeyId, config.AccessKeySecret)
|
||||
cfg := oss.LoadDefaultConfig().
|
||||
WithCredentialsProvider(cred).
|
||||
WithRegion(config.Region)
|
||||
|
||||
ossClient := oss.NewClient(cfg)
|
||||
return &OSSClient{
|
||||
client: ossClient,
|
||||
config: &config,
|
||||
}, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
|
||||
type PolicyToken struct {
|
||||
Policy string `json:"policy"`
|
||||
SecurityToken string `json:"security_token"`
|
||||
SignatureVersion string `json:"x_oss_signature_version"`
|
||||
Credential string `json:"x_oss_credential"`
|
||||
Date string `json:"x_oss_date"`
|
||||
Signature string `json:"signature"`
|
||||
Host string `json:"host"`
|
||||
Dir string `json:"dir"`
|
||||
Callback string `json:"callback"`
|
||||
}
|
||||
|
||||
type CallbackParam struct {
|
||||
CallbackUrl string `json:"callbackUrl"`
|
||||
CallbackBody string `json:"callbackBody"`
|
||||
CallbackBodyType string `json:"callbackBodyType"`
|
||||
}
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
package ali
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"hash"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aliyun/credentials-go/credentials"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (c *Config) GetToken(path string) (*PolicyToken, error) {
|
||||
product := "oss"
|
||||
|
||||
host := fmt.Sprintf("https://%s.oss-%s.aliyuncs.com", c.Bucket, c.Region)
|
||||
if c.Host != nil {
|
||||
host = *c.Host
|
||||
}
|
||||
|
||||
// 设置上传目录
|
||||
dir := strings.TrimRight(path, "/") + "/"
|
||||
|
||||
// callbackUrl为 上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实信息。
|
||||
|
||||
config := new(credentials.Config).
|
||||
SetType("access_key").
|
||||
SetAccessKeyId(c.AccessKeyId).
|
||||
SetAccessKeySecret(c.AccessKeySecret).
|
||||
SetPolicy("").
|
||||
SetRoleSessionExpiration(3600)
|
||||
// SetType("ram_role_arn").
|
||||
// SetRoleArn(os.Getenv("OSS_STS_ROLE_ARN")).
|
||||
// SetRoleSessionName("Role_Session_Name").
|
||||
|
||||
// 根据配置创建凭证提供器
|
||||
provider, err := credentials.NewCredential(config)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "NewCredential fail")
|
||||
}
|
||||
|
||||
// 从凭证提供器获取凭证
|
||||
cred, err := provider.GetCredential()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetCredential fail")
|
||||
}
|
||||
|
||||
// 构建policy
|
||||
utcTime := time.Now().UTC()
|
||||
date := utcTime.Format("20060102")
|
||||
expiration := utcTime.Add(1 * time.Hour)
|
||||
policyMap := map[string]any{
|
||||
"expiration": expiration.Format("2006-01-02T15:04:05.000Z"),
|
||||
"conditions": []any{
|
||||
map[string]string{"bucket": c.Bucket},
|
||||
map[string]string{"x-oss-signature-version": "OSS4-HMAC-SHA256"},
|
||||
map[string]string{"x-oss-credential": fmt.Sprintf("%v/%v/%v/%v/aliyun_v4_request", *cred.AccessKeyId, date, c.Region, product)},
|
||||
map[string]string{"x-oss-date": utcTime.Format("20060102T150405Z")},
|
||||
map[string]string{"x-oss-security-token": *cred.SecurityToken},
|
||||
},
|
||||
}
|
||||
|
||||
// 将policy转换为 JSON 格式
|
||||
policy, err := json.Marshal(policyMap)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "json.Marshal fail")
|
||||
}
|
||||
|
||||
// 构造待签名字符串(StringToSign)
|
||||
stringToSign := base64.StdEncoding.EncodeToString([]byte(policy))
|
||||
|
||||
hmacHash := func() hash.Hash { return sha256.New() }
|
||||
// 构建signing key
|
||||
signingKey := "aliyun_v4" + *cred.AccessKeySecret
|
||||
h1 := hmac.New(hmacHash, []byte(signingKey))
|
||||
io.WriteString(h1, date)
|
||||
h1Key := h1.Sum(nil)
|
||||
|
||||
h2 := hmac.New(hmacHash, h1Key)
|
||||
io.WriteString(h2, c.Region)
|
||||
h2Key := h2.Sum(nil)
|
||||
|
||||
h3 := hmac.New(hmacHash, h2Key)
|
||||
io.WriteString(h3, product)
|
||||
h3Key := h3.Sum(nil)
|
||||
|
||||
h4 := hmac.New(hmacHash, h3Key)
|
||||
io.WriteString(h4, "aliyun_v4_request")
|
||||
h4Key := h4.Sum(nil)
|
||||
|
||||
// 生成签名
|
||||
h := hmac.New(hmacHash, h4Key)
|
||||
io.WriteString(h, stringToSign)
|
||||
signature := hex.EncodeToString(h.Sum(nil))
|
||||
|
||||
var callbackParam CallbackParam
|
||||
callbackParam.CallbackUrl = c.CallbackURL
|
||||
callbackParam.CallbackBody = "filename=${object}&size=${size}&mimeType=${mimeType}"
|
||||
callbackParam.CallbackBodyType = "application/x-www-form-urlencoded"
|
||||
callback_str, err := json.Marshal(callbackParam)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "callback json err:")
|
||||
}
|
||||
callbackBase64 := base64.StdEncoding.EncodeToString(callback_str)
|
||||
// 构建返回给前端的表单
|
||||
return &PolicyToken{
|
||||
Policy: stringToSign,
|
||||
SecurityToken: *cred.SecurityToken,
|
||||
SignatureVersion: "OSS4-HMAC-SHA256",
|
||||
Credential: fmt.Sprintf("%v/%v/%v/%v/aliyun_v4_request", *cred.AccessKeyId, date, c.Region, product),
|
||||
Date: utcTime.UTC().Format("20060102T150405Z"),
|
||||
Signature: signature,
|
||||
Host: host, // 返回 OSS 上传地址
|
||||
Dir: dir, // 返回上传目录
|
||||
Callback: callbackBase64, // 返回上传回调参数
|
||||
}, nil
|
||||
}
|
||||
27
backend/providers/ali/oss_client.go
Normal file
27
backend/providers/ali/oss_client.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package ali
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||||
)
|
||||
|
||||
type OSSClient struct {
|
||||
client *oss.Client
|
||||
config *Config
|
||||
}
|
||||
|
||||
func (c *OSSClient) GetClient() *oss.Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
func (c *OSSClient) PreSignUpload(ctx context.Context, path string) (*oss.PresignResult, error) {
|
||||
request := &oss.PutObjectRequest{
|
||||
Bucket: oss.Ptr(c.config.Bucket),
|
||||
Key: oss.Ptr("quyun/" + strings.Trim(path, "/")),
|
||||
}
|
||||
log.Printf("%+v", request)
|
||||
return c.client.Presign(ctx, request)
|
||||
}
|
||||
Reference in New Issue
Block a user