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
2.1 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "net/http"
  6. )
  7. // BucketCORSRule is the rule of BucketCORS
  8. type BucketCORSRule struct {
  9. ID string `xml:"ID,omitempty"`
  10. AllowedMethods []string `xml:"AllowedMethod"`
  11. AllowedOrigins []string `xml:"AllowedOrigin"`
  12. AllowedHeaders []string `xml:"AllowedHeader,omitempty"`
  13. MaxAgeSeconds int `xml:"MaxAgeSeconds,omitempty"`
  14. ExposeHeaders []string `xml:"ExposeHeader,omitempty"`
  15. }
  16. // BucketGetCORSResult is the result of GetBucketCORS
  17. type BucketGetCORSResult struct {
  18. XMLName xml.Name `xml:"CORSConfiguration"`
  19. Rules []BucketCORSRule `xml:"CORSRule,omitempty"`
  20. }
  21. // GetCORS 实现 Bucket 跨域访问配置读取。
  22. //
  23. // https://www.qcloud.com/document/product/436/8274
  24. func (s *BucketService) GetCORS(ctx context.Context) (*BucketGetCORSResult, *Response, error) {
  25. var res BucketGetCORSResult
  26. sendOpt := sendOptions{
  27. baseURL: s.client.BaseURL.BucketURL,
  28. uri: "/?cors",
  29. method: http.MethodGet,
  30. result: &res,
  31. }
  32. resp, err := s.client.send(ctx, &sendOpt)
  33. return &res, resp, err
  34. }
  35. // BucketPutCORSOptions is the option of PutBucketCORS
  36. type BucketPutCORSOptions struct {
  37. XMLName xml.Name `xml:"CORSConfiguration"`
  38. Rules []BucketCORSRule `xml:"CORSRule,omitempty"`
  39. }
  40. // PutCORS 实现 Bucket 跨域访问设置,您可以通过传入XML格式的配置文件实现配置,文件大小限制为64 KB。
  41. //
  42. // https://www.qcloud.com/document/product/436/8279
  43. func (s *BucketService) PutCORS(ctx context.Context, opt *BucketPutCORSOptions) (*Response, error) {
  44. sendOpt := sendOptions{
  45. baseURL: s.client.BaseURL.BucketURL,
  46. uri: "/?cors",
  47. method: http.MethodPut,
  48. body: opt,
  49. }
  50. resp, err := s.client.send(ctx, &sendOpt)
  51. return resp, err
  52. }
  53. // DeleteCORS 实现 Bucket 跨域访问配置删除。
  54. //
  55. // https://www.qcloud.com/document/product/436/8283
  56. func (s *BucketService) DeleteCORS(ctx context.Context) (*Response, error) {
  57. sendOpt := sendOptions{
  58. baseURL: s.client.BaseURL.BucketURL,
  59. uri: "/?cors",
  60. method: http.MethodDelete,
  61. }
  62. resp, err := s.client.send(ctx, &sendOpt)
  63. return resp, err
  64. }