You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

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