Skip to content

Commit

Permalink
Merge pull request #301 from tencentyun/feature_jojoliang_c58c97cc
Browse files Browse the repository at this point in the history
Feature jojoliang c58c97cc
  • Loading branch information
agin719 authored Feb 25, 2025
2 parents 2e3ac63 + 2fc78aa commit 2c14e69
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
15 changes: 10 additions & 5 deletions bucket_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,24 @@ type BucketGetPolicyResult BucketPutPolicyOptions

func (s *BucketService) PutPolicy(ctx context.Context, opt *BucketPutPolicyOptions) (*Response, error) {
var f *strings.Reader
var body string
if opt != nil {
bs, err := json.Marshal(opt)
if err != nil {
return nil, err
}
body := string(bs)
body = string(bs)
f = strings.NewReader(body)
}
header := &commonHeader{
ContentLength: int64(len(body)),
}
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?policy",
method: http.MethodPut,
body: f,
baseURL: s.client.BaseURL.BucketURL,
uri: "/?policy",
method: http.MethodPut,
body: f,
optHeader: header,
}
resp, err := s.client.send(ctx, sendOpt)
return resp, err
Expand Down
26 changes: 26 additions & 0 deletions cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,30 @@ func (c *Client) GetCredential() *Credential {
return nil
}

type commonHeader struct {
ContentLength int64 `header:"Content-Length,omitempty"`
}

func (c *Client) newPresignedRequest(ctx context.Context, sendOpt *sendOptions) (req *http.Request, err error) {
sendOpt.uri, err = addURLOptions(sendOpt.uri, sendOpt.optQuery)
if err != nil {
return
}
u, _ := url.Parse(sendOpt.uri)
urlStr := sendOpt.baseURL.ResolveReference(u).String()

req, err = http.NewRequest(sendOpt.method, urlStr, nil)
if err != nil {
return
}

req.Header, err = addHeaderOptions(ctx, req.Header, sendOpt.optHeader)
if err != nil {
return
}
return req, err
}

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) {
if c.invalidURL {
return nil, invalidBucketErr
Expand Down Expand Up @@ -302,6 +326,8 @@ func (c *Client) newRequest(ctx context.Context, baseURL *url.URL, uri, method s
contentMD5 = base64.StdEncoding.EncodeToString(calMD5Digest(b))
contentLength = int64(len(b))
}
} else if method == http.MethodPut || method == http.MethodPost {
contentLength = 0
}

req, err = http.NewRequest(method, urlStr, reader)
Expand Down
8 changes: 4 additions & 4 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
}
}
}
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
req, err := s.client.newPresignedRequest(ctx, &sendOpt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -243,7 +243,7 @@ func (s *ObjectService) GetPresignedURL2(ctx context.Context, httpMethod, name s
sendOpt.uri = fmt.Sprintf("%s%s%s", sendOpt.uri, mark, url.Values{"x-cos-security-token": []string{cred.SessionToken}}.Encode())
}

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

req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
req, err := s.client.newPresignedRequest(ctx, &sendOpt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -382,7 +382,7 @@ func (s *ObjectService) GetSignature(ctx context.Context, httpMethod, name, ak,
sendOpt.uri = fmt.Sprintf("%s?%s", sendOpt.uri, qs)
}
}
req, err := s.client.newRequest(ctx, sendOpt.baseURL, sendOpt.uri, sendOpt.method, sendOpt.body, sendOpt.optQuery, sendOpt.optHeader, false)
req, err := s.client.newPresignedRequest(ctx, &sendOpt)
if err != nil {
return ""
}
Expand Down

0 comments on commit 2c14e69

Please sign in to comment.