-
Notifications
You must be signed in to change notification settings - Fork 92
/
bucket_policy.go
72 lines (65 loc) · 1.92 KB
/
bucket_policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cos
import (
"bytes"
"context"
"encoding/json"
"net/http"
"strings"
)
type BucketStatement struct {
Principal map[string][]string `json:"principal,omitempty"`
Action []string `json:"action,omitempty"`
Effect string `json:"effect,omitempty"`
Resource []string `json:"resource,omitempty"`
Condition map[string]map[string]interface{} `json:"condition,omitempty"`
Sid string `json:"sid,omitempty"`
}
type BucketPutPolicyOptions struct {
Statement []BucketStatement `json:"statement,omitempty"`
Version string `json:"version,omitempty"`
Principal map[string][]string `json:"principal,omitempty"`
}
type BucketGetPolicyResult BucketPutPolicyOptions
func (s *BucketService) PutPolicy(ctx context.Context, opt *BucketPutPolicyOptions) (*Response, error) {
var f *strings.Reader
if opt != nil {
bs, err := json.Marshal(opt)
if err != nil {
return nil, err
}
body := string(bs)
f = strings.NewReader(body)
}
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?policy",
method: http.MethodPut,
body: f,
}
resp, err := s.client.send(ctx, sendOpt)
return resp, err
}
func (s *BucketService) GetPolicy(ctx context.Context) (*BucketGetPolicyResult, *Response, error) {
var bs bytes.Buffer
var res BucketGetPolicyResult
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?policy",
method: http.MethodGet,
result: &bs,
}
resp, err := s.client.doRetry(ctx, sendOpt)
if err == nil {
err = json.Unmarshal(bs.Bytes(), &res)
}
return &res, resp, err
}
func (s *BucketService) DeletePolicy(ctx context.Context) (*Response, error) {
sendOpt := &sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?policy",
method: http.MethodDelete,
}
resp, err := s.client.doRetry(ctx, sendOpt)
return resp, err
}