Skip to content

Commit 1c9f083

Browse files
authored
Merge pull request #232 from tencentyun/feature_jojoliang_6052914a
update GetPresignedURL
2 parents 92a2e31 + a98d344 commit 1c9f083

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

cos.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
const (
2626
// Version current go sdk version
27-
Version = "0.7.46"
27+
Version = "0.7.47"
2828
UserAgent = "cos-go-sdk-v5/" + Version
2929
contentTypeXML = "application/xml"
3030
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
@@ -505,6 +505,9 @@ func addHeaderOptions(ctx context.Context, header http.Header, opt interface{})
505505
}
506506

507507
func checkURL(baseURL *url.URL) bool {
508+
if baseURL == nil {
509+
return false
510+
}
508511
host := baseURL.String()
509512
if hostSuffix.MatchString(host) && !hostPrefix.MatchString(host) {
510513
return false

object.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,7 @@ type PresignedURLOptions struct {
124124
// GetPresignedURL get the object presigned to down or upload file by url
125125
// 预签名函数,signHost: 默认签入Header Host, 您也可以选择不签入Header Host,但可能导致请求失败或安全漏洞
126126
func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt interface{}, signHost ...bool) (*url.URL, error) {
127-
// 兼容 name 以 / 开头的情况
128-
if strings.HasPrefix(name, "/") {
129-
name = encodeURIComponent("/") + encodeURIComponent(name[1:], []byte{'/'})
130-
} else {
131-
name = encodeURIComponent(name, []byte{'/'})
132-
}
127+
name = encodeURIComponent(name)
133128

134129
sendOpt := sendOptions{
135130
baseURL: s.client.BaseURL.BucketURL,
@@ -192,12 +187,7 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
192187
}
193188

194189
func (s *ObjectService) GetPresignedURL2(ctx context.Context, httpMethod, name string, expired time.Duration, opt interface{}, signHost ...bool) (*url.URL, error) {
195-
// 兼容 name 以 / 开头的情况
196-
if strings.HasPrefix(name, "/") {
197-
name = encodeURIComponent("/") + encodeURIComponent(name[1:], []byte{'/'})
198-
} else {
199-
name = encodeURIComponent(name, []byte{'/'})
200-
}
190+
name = encodeURIComponent(name)
201191

202192
cred := s.client.GetCredential()
203193
if cred == nil {
@@ -271,11 +261,7 @@ func (s *ObjectService) GetPresignedURL2(ctx context.Context, httpMethod, name s
271261

272262
func (s *ObjectService) GetSignature(ctx context.Context, httpMethod, name, ak, sk string, expired time.Duration, opt *PresignedURLOptions, signHost ...bool) string {
273263
// 兼容 name 以 / 开头的情况
274-
if strings.HasPrefix(name, "/") {
275-
name = encodeURIComponent("/") + encodeURIComponent(name[1:], []byte{'/'})
276-
} else {
277-
name = encodeURIComponent(name, []byte{'/'})
278-
}
264+
name = encodeURIComponent(name)
279265

280266
sendOpt := sendOptions{
281267
baseURL: s.client.BaseURL.BucketURL,

0 commit comments

Comments
 (0)