Skip to content

Commit

Permalink
Merge pull request #284 from tencentyun/feature_jojoliang_6aaec5dc
Browse files Browse the repository at this point in the history
更新GetPresignedURL3, 兼容trpc-go url格式
  • Loading branch information
agin719 authored Nov 4, 2024
2 parents 43fbd84 + 90a859c commit b95652e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
6 changes: 4 additions & 2 deletions cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

const (
// Version current go sdk version
Version = "0.7.56"
Version = "0.7.57"
UserAgent = "cos-go-sdk-v5/" + Version
contentTypeXML = "application/xml"
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
Expand Down Expand Up @@ -96,7 +96,9 @@ func (*BaseURL) innerCheck(u *url.URL, reg *regexp.Regexp) bool {
}

func (u *BaseURL) Check() bool {
return u.innerCheck(u.BucketURL, bucketDomainChecker) && u.innerCheck(u.ServiceURL, serviceDomainChecker) && u.innerCheck(u.BatchURL, batchDomainChecker)
return u.innerCheck(u.BucketURL, bucketDomainChecker) &&
(u.innerCheck(u.ServiceURL, serviceDomainChecker) || u.innerCheck(u.ServiceURL, bucketDomainChecker)) &&
(u.innerCheck(u.BatchURL, batchDomainChecker) || u.innerCheck(u.BatchURL, bucketDomainChecker))
}

// NewBucketURL 生成 BaseURL 所需的 BucketURL
Expand Down
24 changes: 12 additions & 12 deletions cos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,52 +247,52 @@ func Test_CheckRetrieable(t *testing.T) {

func Test_BaseURL(t *testing.T) {
u, _ := url.Parse("https://example-125000000.cos.ap-chengdu.myqcloud.com")
if !(&BaseURL{BucketURL: u}).Check() {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://example-125000000.cos-website.ap-chengdu.myqcloud.com")
if !(&BaseURL{BucketURL: u}).Check() {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://example-125000000.cos-internal.ap-chengdu.tencentcos.cn")
if !(&BaseURL{BucketURL: u}).Check() {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://example-125000000.cos.ap-chengdu.tencentcos.cn")
if !(&BaseURL{BucketURL: u}).Check() {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://example-125000000.cos.accelerate.myqcloud.com")
if !(&BaseURL{BucketURL: u}).Check() {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://example-125000000.cos-internal.accelerate.tencentcos.cn")
if !(&BaseURL{BucketURL: u}).Check() {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("http://example-125000000.cos.ap-chengdu.myqcloud.com:8080")
if !(&BaseURL{BucketURL: u}).Check() {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("http://example-125000000.cos-internal.ap-chengdu.tencentcos.cn:80")
if !(&BaseURL{BucketURL: u}).Check() {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://example-125000000.cos-internal.accelerate.tencentcos.cn:443")
if !(&BaseURL{BucketURL: u}).Check() {
if !(&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}

u, _ = url.Parse("https://[email protected]/.myqcloud.com")
if (&BaseURL{BucketURL: u}).Check() {
if (&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://[email protected]/.myqcloud.com:443")
if (&BaseURL{BucketURL: u}).Check() {
if (&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}
u, _ = url.Parse("https://[email protected]/myqcloud.com")
if (&BaseURL{BucketURL: u}).Check() {
if (&BaseURL{BucketURL: u, ServiceURL: u, BatchURL: u}).Check() {
t.Errorf("BaseURL check failed: %v", u)
}

Expand Down
21 changes: 16 additions & 5 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ func (s *ObjectService) GetObjectURL(name string) *url.URL {
}

type PresignedURLOptions struct {
Query *url.Values `xml:"-" url:"-" header:"-"`
Header *http.Header `header:"-,omitempty" url:"-" xml:"-"`
SignMerged bool `xml:"-" url:"-" header:"-"`
AuthTime *AuthTime `xml:"-" url:"-" header:"-"`
Query *url.Values `xml:"-" url:"-" header:"-"`
Header *http.Header `header:"-,omitempty" url:"-" xml:"-"`
SignMerged bool `xml:"-" url:"-" header:"-"`
AuthTime *AuthTime `xml:"-" url:"-" header:"-"`
EncodeDelimiter bool `xml:"-" url:"-" header:"-"`
}

// GetPresignedURL get the object presigned to down or upload file by url
Expand Down Expand Up @@ -282,7 +283,17 @@ func (s *ObjectService) GetPresignedURL3(ctx context.Context, httpMethod, name s
if name == "" {
return nil, fmt.Errorf("object key is empty.")
}
name = encodeURIComponent(name, []byte("/"))
var encodeDelimiter bool
if opt != nil {
if popt, ok := opt.(*PresignedURLOptions); ok {
encodeDelimiter = popt.EncodeDelimiter
}
}
if encodeDelimiter {
name = encodeURIComponent(name)
} else {
name = encodeURIComponent(name, []byte("/"))
}

cred := s.client.GetCredential()
if cred == nil {
Expand Down
1 change: 1 addition & 0 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ func TestObjectService_GetPresignedURL3(t *testing.T) {
t.Fatalf("Wrong PreSignedURL!")
}

opt1.EncodeDelimiter = true
_, err = client.Object.GetPresignedURL3(c, http.MethodPut, "", time.Hour, opt1)
if err == nil {
t.Errorf("GetPresignedURL expect err is not null")
Expand Down

0 comments on commit b95652e

Please sign in to comment.