From 61f0d6f54ff20006522b4248b4bf2aed13bccae4 Mon Sep 17 00:00:00 2001 From: tu6ge Date: Tue, 1 Apr 2025 21:01:34 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E6=9B=B4=E6=8D=A2=20oss=20?= =?UTF-8?q?=E7=9A=84=20sdk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.sample.toml | 2 +- config.sample.zh-CN.toml | 2 +- go.mod | 3 ++- go.sum | 4 ++++ lib/aliyunoss/uploader.go | 17 ++++------------- 5 files changed, 12 insertions(+), 16 deletions(-) diff --git a/config.sample.toml b/config.sample.toml index 5a35c0f..fbfe924 100644 --- a/config.sample.toml +++ b/config.sample.toml @@ -110,7 +110,7 @@ url_format = "{endpoint}/{bucket}/{path}" # AliyunOSS Uploader [uploaders.aliyunoss] -endpoint = "https://oss-cn-shanghai.aliyuncs.com" +endpoint = "cn-shanghai" access_key_id = "your-access-key-id" access_key_secret = "your-access-key-secret" bucket_name = "your-bucket-name" diff --git a/config.sample.zh-CN.toml b/config.sample.zh-CN.toml index c30dc9b..9dd13b5 100644 --- a/config.sample.zh-CN.toml +++ b/config.sample.zh-CN.toml @@ -115,7 +115,7 @@ url_format = "{endpoint}/{bucket}/{path}" # 阿里云 OSS [uploaders.aliyunoss] -endpoint = "https://oss-cn-shanghai.aliyuncs.com" +endpoint = "cn-shanghai" access_key_id = "your-access-key-id" access_key_secret = "your-access-key-secret" bucket_name = "your-bucket-name" diff --git a/go.mod b/go.mod index 924cab6..04fb053 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.18 require ( github.com/alexflint/go-arg v1.4.3 - github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible github.com/aws/aws-sdk-go v1.54.6 github.com/fatih/color v1.13.0 github.com/mitchellh/mapstructure v1.4.3 @@ -17,8 +16,10 @@ require ( require ( github.com/alexflint/go-scalar v1.1.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect + github.com/tu6ge/oss-go v0.1.2 // indirect golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect golang.org/x/mobile v0.0.0-20220224134551-8a0a1e50732f // indirect golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect diff --git a/go.sum b/go.sum index 2b70da3..c4ac753 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= @@ -37,6 +39,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/tu6ge/oss-go v0.1.2 h1:NpvS71drHyD5COv8j8z4fGSMnKlw3mcImujzzmppO8w= +github.com/tu6ge/oss-go v0.1.2/go.mod h1:ZyfQ1WVE6b6QmoN0o9BnkwaniuXGuzV1CRRoO/OZHE8= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.design/x/clipboard v0.6.0 h1:+U/e2KDBdpIjkRdxO8GwlD6dKD3Jx5zlNNzQjxte4A0= diff --git a/lib/aliyunoss/uploader.go b/lib/aliyunoss/uploader.go index 98c0382..5d755a2 100644 --- a/lib/aliyunoss/uploader.go +++ b/lib/aliyunoss/uploader.go @@ -2,14 +2,13 @@ package aliyunoss import ( "fmt" - "os" "path/filepath" "time" - "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/pluveto/upgit/lib/model" "github.com/pluveto/upgit/lib/xapp" "github.com/pluveto/upgit/lib/xlog" + "github.com/tu6ge/oss-go" ) type OSSConfig struct { @@ -56,21 +55,13 @@ func (u *OSSUploader) buildUrl(path string) string { } func (u *OSSUploader) PutFile(localPath, targetPath string) (err error) { - cli, err := oss.New(u.Config.Endpoint, u.Config.AccessKeyId, u.Config.AccessKeySecret) + cli, err := oss.New(u.Config.AccessKeyId, u.Config.AccessKeySecret, u.Config.BucketName, u.Config.Endpoint) if err != nil { return err } - bucket, err := cli.Bucket(u.Config.BucketName) - if err != nil { - return err - } - - file, err := os.OpenFile(localPath, os.O_RDONLY, 0644) - if err != nil { - return err - } + obj := oss.NewObject(targetPath) - err = bucket.PutObject(targetPath, file) + err = obj.FilePath(localPath).ContentType("").Upload(&cli) return } From d74264e1cd3e55191fc22a9a0101d761fb377c4b Mon Sep 17 00:00:00 2001 From: tu6ge Date: Tue, 1 Apr 2025 21:23:40 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E6=94=AF=E6=8C=81=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.sample.toml | 2 +- config.sample.zh-CN.toml | 2 +- lib/aliyunoss/uploader.go | 9 ++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/config.sample.toml b/config.sample.toml index fbfe924..5a35c0f 100644 --- a/config.sample.toml +++ b/config.sample.toml @@ -110,7 +110,7 @@ url_format = "{endpoint}/{bucket}/{path}" # AliyunOSS Uploader [uploaders.aliyunoss] -endpoint = "cn-shanghai" +endpoint = "https://oss-cn-shanghai.aliyuncs.com" access_key_id = "your-access-key-id" access_key_secret = "your-access-key-secret" bucket_name = "your-bucket-name" diff --git a/config.sample.zh-CN.toml b/config.sample.zh-CN.toml index 9dd13b5..c30dc9b 100644 --- a/config.sample.zh-CN.toml +++ b/config.sample.zh-CN.toml @@ -115,7 +115,7 @@ url_format = "{endpoint}/{bucket}/{path}" # 阿里云 OSS [uploaders.aliyunoss] -endpoint = "cn-shanghai" +endpoint = "https://oss-cn-shanghai.aliyuncs.com" access_key_id = "your-access-key-id" access_key_secret = "your-access-key-secret" bucket_name = "your-bucket-name" diff --git a/lib/aliyunoss/uploader.go b/lib/aliyunoss/uploader.go index 5d755a2..7139ef5 100644 --- a/lib/aliyunoss/uploader.go +++ b/lib/aliyunoss/uploader.go @@ -3,6 +3,7 @@ package aliyunoss import ( "fmt" "path/filepath" + "strings" "time" "github.com/pluveto/upgit/lib/model" @@ -55,7 +56,13 @@ func (u *OSSUploader) buildUrl(path string) string { } func (u *OSSUploader) PutFile(localPath, targetPath string) (err error) { - cli, err := oss.New(u.Config.AccessKeyId, u.Config.AccessKeySecret, u.Config.BucketName, u.Config.Endpoint) + endpoint := u.Config.Endpoint + + endpoint = strings.Replace(endpoint, "https://oss-", "", 1) + endpoint = strings.Replace(endpoint, "http://oss-", "", 1) + endpoint = strings.Replace(endpoint, ".aliyuncs.com", "", 1) + + cli, err := oss.New(u.Config.AccessKeyId, u.Config.AccessKeySecret, u.Config.BucketName, endpoint) if err != nil { return err } From 9d93769da9835946c879e44c636f47743076f0e9 Mon Sep 17 00:00:00 2001 From: tu6ge Date: Sat, 12 Apr 2025 09:20:13 +0800 Subject: [PATCH 3/3] feat: Support custom endpoint domain --- go.mod | 3 +-- go.sum | 8 ++------ lib/aliyunoss/uploader.go | 11 +++++------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 04fb053..68f63d3 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/fatih/color v1.13.0 github.com/mitchellh/mapstructure v1.4.3 github.com/pelletier/go-toml/v2 v2.0.6 + github.com/tu6ge/oss-go v0.1.7 golang.design/x/clipboard v0.6.0 golang.org/x/image v0.0.0-20220302094943-723b81ca9867 gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0 @@ -19,9 +20,7 @@ require ( github.com/joho/godotenv v1.5.1 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect - github.com/tu6ge/oss-go v0.1.2 // indirect golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect golang.org/x/mobile v0.0.0-20220224134551-8a0a1e50732f // indirect golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect - golang.org/x/time v0.11.0 // indirect ) diff --git a/go.sum b/go.sum index c4ac753..41df41e 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,6 @@ github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/u github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA= github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM= github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o= -github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible h1:8psS8a+wKfiLt1iVDX79F7Y6wUM49Lcha2FMXt4UM8g= -github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= github.com/aws/aws-sdk-go v1.54.6 h1:HEYUib3yTt8E6vxjMWM3yAq5b+qjj/6aKA62mkgux9g= github.com/aws/aws-sdk-go v1.54.6/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -39,8 +37,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/tu6ge/oss-go v0.1.2 h1:NpvS71drHyD5COv8j8z4fGSMnKlw3mcImujzzmppO8w= -github.com/tu6ge/oss-go v0.1.2/go.mod h1:ZyfQ1WVE6b6QmoN0o9BnkwaniuXGuzV1CRRoO/OZHE8= +github.com/tu6ge/oss-go v0.1.7 h1:dFoOYMx88pNFUDbDZZndLE66pbouvUqR3y/Av9xaQ8o= +github.com/tu6ge/oss-go v0.1.7/go.mod h1:ZyfQ1WVE6b6QmoN0o9BnkwaniuXGuzV1CRRoO/OZHE8= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.design/x/clipboard v0.6.0 h1:+U/e2KDBdpIjkRdxO8GwlD6dKD3Jx5zlNNzQjxte4A0= @@ -85,8 +83,6 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0= -golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= diff --git a/lib/aliyunoss/uploader.go b/lib/aliyunoss/uploader.go index 7139ef5..d8046c9 100644 --- a/lib/aliyunoss/uploader.go +++ b/lib/aliyunoss/uploader.go @@ -3,7 +3,6 @@ package aliyunoss import ( "fmt" "path/filepath" - "strings" "time" "github.com/pluveto/upgit/lib/model" @@ -58,11 +57,11 @@ func (u *OSSUploader) buildUrl(path string) string { func (u *OSSUploader) PutFile(localPath, targetPath string) (err error) { endpoint := u.Config.Endpoint - endpoint = strings.Replace(endpoint, "https://oss-", "", 1) - endpoint = strings.Replace(endpoint, "http://oss-", "", 1) - endpoint = strings.Replace(endpoint, ".aliyuncs.com", "", 1) - - cli, err := oss.New(u.Config.AccessKeyId, u.Config.AccessKeySecret, u.Config.BucketName, endpoint) + cli, err := oss.New(u.Config.AccessKeyId, u.Config.AccessKeySecret, u.Config.BucketName, "cn-shanghai") + if err != nil { + return err + } + err = cli.Bucket.SetEndPointDomain(endpoint) if err != nil { return err }