From 1e2d00edaf889093bb0042855465a70141ca359b Mon Sep 17 00:00:00 2001 From: lzh <18320341470> Date: Wed, 16 Jul 2025 14:55:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=BF=E9=87=8C=E4=BA=91oss?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oss_tool/aliyun_oss.go | 19 ++++++++++++++++--- oss_tool/aliyun_oss_test.go | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/oss_tool/aliyun_oss.go b/oss_tool/aliyun_oss.go index 065a91d..6c489e7 100644 --- a/oss_tool/aliyun_oss.go +++ b/oss_tool/aliyun_oss.go @@ -40,9 +40,9 @@ func (c *ALiYunOSSClient) GetSignUrl(bucket string, key string, expires time.Dur result, err = c.ossClient.Presign( context.Background(), &oss.PutObjectRequest{ - Bucket: oss.Ptr(bucket), - Key: oss.Ptr(key), - //ContentType: oss.Ptr("application/octet-stream"), + Bucket: oss.Ptr(bucket), + Key: oss.Ptr(key), + ContentType: oss.Ptr("application/octet-stream"), }, oss.PresignExpires(expires), ) @@ -52,6 +52,19 @@ func (c *ALiYunOSSClient) GetSignUrl(bucket string, key string, expires time.Dur return } +func (c *ALiYunOSSClient) GetSignUrlByPutObjectRequest(req *oss.PutObjectRequest, expires time.Duration) (result *oss.PresignResult, err error) { + // 生成PutObject的预签名URL + result, err = c.ossClient.Presign( + context.Background(), + req, + oss.PresignExpires(expires), + ) + if err != nil { + return nil, err + } + return +} + // PutForLocalFile 上传本地文件 func (c *ALiYunOSSClient) PutForLocalFile(bucket, key, path string) (result *oss.PutObjectResult, err error) { // 创建上传对象的请求 diff --git a/oss_tool/aliyun_oss_test.go b/oss_tool/aliyun_oss_test.go index 637c244..878deaa 100644 --- a/oss_tool/aliyun_oss_test.go +++ b/oss_tool/aliyun_oss_test.go @@ -1,6 +1,7 @@ package oss_tool import ( + "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss" "os" "testing" ) @@ -36,6 +37,22 @@ func TestALiYunOSSClient_GetSignUrl(t *testing.T) { t.Log(sign) } +func TestALiYunOSSClient_GetSignUrlByPutObjectRequest(t *testing.T) { + err := client.NewAliYunOSS() + if err != nil { + t.Error(err) + } + req := &oss.PutObjectRequest{} + req.Bucket = oss.Ptr("") + req.Key = oss.Ptr("test/upload/bizhi2.jpg") + req.ContentType = oss.Ptr("application/octet-stream") + sign, err := client.GetSignUrlByPutObjectRequest(req, 0) + if err != nil { + t.Error(err) + } + t.Log(sign) +} + func TestALiYunOSSClient_PutForLocalFile(t *testing.T) { err := client.NewAliYunOSS() if err != nil {