Skip to content

Commit 2c14e69

Browse files
authored
Merge pull request #301 from tencentyun/feature_jojoliang_c58c97cc
Feature jojoliang c58c97cc
2 parents 2e3ac63 + 2fc78aa commit 2c14e69

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

bucket_policy.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,24 @@ type BucketGetPolicyResult BucketPutPolicyOptions
2727

2828
func (s *BucketService) PutPolicy(ctx context.Context, opt *BucketPutPolicyOptions) (*Response, error) {
2929
var f *strings.Reader
30+
var body string
3031
if opt != nil {
3132
bs, err := json.Marshal(opt)
3233
if err != nil {
3334
return nil, err
3435
}
35-
body := string(bs)
36+
body = string(bs)
3637
f = strings.NewReader(body)
3738
}
39+
header := &commonHeader{
40+
ContentLength: int64(len(body)),
41+
}
3842
sendOpt := &sendOptions{
39-
baseURL: s.client.BaseURL.BucketURL,
40-
uri: "/?policy",
41-
method: http.MethodPut,
42-
body: f,
43+
baseURL: s.client.BaseURL.BucketURL,
44+
uri: "/?policy",
45+
method: http.MethodPut,
46+
body: f,
47+
optHeader: header,
4348
}
4449
resp, err := s.client.send(ctx, sendOpt)
4550
return resp, err

cos.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,30 @@ func (c *Client) GetCredential() *Credential {
267267
return nil
268268
}
269269

270+
type commonHeader struct {
271+
ContentLength int64 `header:"Content-Length,omitempty"`
272+
}
273+
274+
func (c *Client) newPresignedRequest(ctx context.Context, sendOpt *sendOptions) (req *http.Request, err error) {
275+
sendOpt.uri, err = addURLOptions(sendOpt.uri, sendOpt.optQuery)
276+
if err != nil {
277+
return
278+
}
279+
u, _ := url.Parse(sendOpt.uri)
280+
urlStr := sendOpt.baseURL.ResolveReference(u).String()
281+
282+
req, err = http.NewRequest(sendOpt.method, urlStr, nil)
283+
if err != nil {
284+
return
285+
}
286+
287+
req.Header, err = addHeaderOptions(ctx, req.Header, sendOpt.optHeader)
288+
if err != nil {
289+
return
290+
}
291+
return req, err
292+
}
293+
270294
func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method string, body interface{}, optQuery interface{}, optHeader interface{}, isRetry bool) (req *http.Request, err error) {
271295
if c.invalidURL {
272296
return nil, invalidBucketErr
@@ -302,6 +326,8 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
302326
contentMD5 = base64.StdEncoding.EncodeToString(calMD5Digest(b))
303327
contentLength = int64(len(b))
304328
}
329+
} else if method == http.MethodPut || method == http.MethodPost {
330+
contentLength = 0
305331
}
306332

307333
req, err = http.NewRequest(method, urlStr, reader)

object.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
162162
}
163163
}
164164
}
165-
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
165+
req, err := s.client.newPresignedRequest(ctx, &sendOpt)
166166
if err != nil {
167167
return nil, err
168168
}
@@ -243,7 +243,7 @@ func (s *ObjectService) GetPresignedURL2(ctx context.Context, httpMethod, name s
243243
sendOpt.uri = fmt.Sprintf("%s%s%s", sendOpt.uri, mark, url.Values{"x-cos-security-token": []string{cred.SessionToken}}.Encode())
244244
}
245245

246-
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
246+
req, err := s.client.newPresignedRequest(ctx, &sendOpt)
247247
if err != nil {
248248
return nil, err
249249
}
@@ -329,7 +329,7 @@ func (s *ObjectService) GetPresignedURL3(ctx context.Context, httpMethod, name s
329329
sendOpt.uri = fmt.Sprintf("%s%s%s", sendOpt.uri, mark, url.Values{"x-cos-security-token": []string{cred.SessionToken}}.Encode())
330330
}
331331

332-
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
332+
req, err := s.client.newPresignedRequest(ctx, &sendOpt)
333333
if err != nil {
334334
return nil, err
335335
}
@@ -382,7 +382,7 @@ func (s *ObjectService) GetSignature(ctx context.Context, httpMethod, name, ak,
382382
sendOpt.uri = fmt.Sprintf("%s?%s", sendOpt.uri, qs)
383383
}
384384
}
385-
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
385+
req, err := s.client.newPresignedRequest(ctx, &sendOpt)
386386
if err != nil {
387387
return ""
388388
}

0 commit comments

Comments
 (0)