Skip to content

Commit 99de629

Browse files
author
jojoliang
committed
预签名URL不编码/
1 parent b537ff2 commit 99de629

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

cos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
const (
2727
// Version current go sdk version
28-
Version = "0.7.49"
28+
Version = "0.7.50"
2929
UserAgent = "cos-go-sdk-v5/" + Version
3030
contentTypeXML = "application/xml"
3131
defaultServiceBaseURL = "http://service.cos.myqcloud.com"

object.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
130130
if name == "" {
131131
return nil, fmt.Errorf("object key is empty.")
132132
}
133-
name = encodeURIComponent(name)
133+
// 兼容 name 以 / 开头的情况
134+
if strings.HasPrefix(name, "/") {
135+
name = encodeURIComponent("/") + encodeURIComponent(name[1:], []byte{'/'})
136+
} else {
137+
name = encodeURIComponent(name, []byte{'/'})
138+
}
134139

135140
sendOpt := sendOptions{
136141
baseURL: s.client.BaseURL.BucketURL,
@@ -196,8 +201,12 @@ func (s *ObjectService) GetPresignedURL2(ctx context.Context, httpMethod, name s
196201
if name == "" {
197202
return nil, fmt.Errorf("object key is empty.")
198203
}
199-
200-
name = encodeURIComponent(name)
204+
// 兼容 name 以 / 开头的情况
205+
if strings.HasPrefix(name, "/") {
206+
name = encodeURIComponent("/") + encodeURIComponent(name[1:], []byte{'/'})
207+
} else {
208+
name = encodeURIComponent(name, []byte{'/'})
209+
}
201210

202211
cred := s.client.GetCredential()
203212
if cred == nil {

object_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ func TestObjectService_GetPresignedURL(t *testing.T) {
258258
if err == nil {
259259
t.Errorf("GetPresignedURL expect err is not null")
260260
}
261+
262+
_, err = client.Object.GetPresignedURL(c, http.MethodPut, "/", ak, sk, time.Hour, opt1)
263+
if err != nil {
264+
t.Errorf("GetPresignedURL return err: %v", err)
265+
}
266+
261267
}
262268

263269
/*
@@ -371,6 +377,11 @@ func TestObjectService_GetPresignedURL2(t *testing.T) {
371377
t.Errorf("GetPresignedURL expect err is not null")
372378
}
373379

380+
_, err = client.Object.GetPresignedURL2(c, http.MethodPut, "/", time.Hour, opt1)
381+
if err != nil {
382+
t.Errorf("GetPresignedURL return err: %v", err)
383+
}
384+
374385
}
375386

376387
func TestObjectService_GetPresignedURL3(t *testing.T) {

0 commit comments

Comments
 (0)