Migrate OSS integration to SDK v2
This commit is contained in:
+42
-22
@@ -1,12 +1,15 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
|
||||
|
||||
"git.beifan.cn/trace-system/backend-go/config"
|
||||
)
|
||||
@@ -25,21 +28,16 @@ func (s OSSService) UploadObject(objectKey string, reader io.Reader, contentType
|
||||
return err
|
||||
}
|
||||
|
||||
client, err := oss.New(normalizeOSSEndpoint(s.cfg.Endpoint), s.cfg.AccessKeyID, s.cfg.AccessKeySecret)
|
||||
if err != nil {
|
||||
return fmt.Errorf("初始化 OSS 客户端失败: %w", err)
|
||||
request := &oss.PutObjectRequest{
|
||||
Bucket: oss.Ptr(s.cfg.Bucket),
|
||||
Key: oss.Ptr(objectKey),
|
||||
Body: reader,
|
||||
}
|
||||
|
||||
bucket, err := client.Bucket(s.cfg.Bucket)
|
||||
if err != nil {
|
||||
return fmt.Errorf("打开 OSS Bucket 失败: %w", err)
|
||||
}
|
||||
|
||||
options := []oss.Option{}
|
||||
if contentType != "" {
|
||||
options = append(options, oss.ContentType(contentType))
|
||||
request.ContentType = oss.Ptr(contentType)
|
||||
}
|
||||
if err := bucket.PutObject(objectKey, reader, options...); err != nil {
|
||||
|
||||
if _, err := s.newClient().PutObject(context.TODO(), request); err != nil {
|
||||
return fmt.Errorf("上传现场图片失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -87,19 +85,22 @@ func (s OSSService) SignedURL(objectKey string) (string, error) {
|
||||
if err := s.validateConfig(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
client, err := oss.New(normalizeOSSEndpoint(s.cfg.Endpoint), s.cfg.AccessKeyID, s.cfg.AccessKeySecret)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("初始化 OSS 客户端失败: %w", err)
|
||||
}
|
||||
bucket, err := client.Bucket(s.cfg.Bucket)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("打开 OSS Bucket 失败: %w", err)
|
||||
}
|
||||
expires := s.cfg.SignedURLExpire
|
||||
if expires <= 0 {
|
||||
expires = 3600
|
||||
}
|
||||
return bucket.SignURL(objectKey, oss.HTTPGet, int64(expires))
|
||||
result, err := s.newClient().Presign(
|
||||
context.TODO(),
|
||||
&oss.GetObjectRequest{
|
||||
Bucket: oss.Ptr(s.cfg.Bucket),
|
||||
Key: oss.Ptr(objectKey),
|
||||
},
|
||||
oss.PresignExpires(time.Duration(expires)*time.Second),
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("生成 OSS 签名 URL 失败: %w", err)
|
||||
}
|
||||
return result.URL, nil
|
||||
}
|
||||
|
||||
func (s OSSService) objectKeyFromRef(objectRef string) string {
|
||||
@@ -138,3 +139,22 @@ func normalizeOSSEndpoint(endpoint string) string {
|
||||
}
|
||||
return "https://" + endpoint
|
||||
}
|
||||
|
||||
func (s OSSService) newClient() *oss.Client {
|
||||
cfg := oss.LoadDefaultConfig().
|
||||
WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
|
||||
s.cfg.AccessKeyID,
|
||||
s.cfg.AccessKeySecret,
|
||||
)).
|
||||
WithRegion(normalizeOSSRegion(s.cfg.Region)).
|
||||
WithEndpoint(strings.TrimPrefix(strings.TrimPrefix(normalizeOSSEndpoint(s.cfg.Endpoint), "https://"), "http://"))
|
||||
return oss.NewClient(cfg)
|
||||
}
|
||||
|
||||
func normalizeOSSRegion(region string) string {
|
||||
region = strings.TrimSpace(region)
|
||||
if strings.HasPrefix(region, "oss-") {
|
||||
return strings.TrimPrefix(region, "oss-")
|
||||
}
|
||||
return region
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user