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