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.

106 lines
3.0 KiB

  1. package cos
  2. import (
  3. "context"
  4. "encoding/xml"
  5. "net/http"
  6. )
  7. // BucketService ...
  8. //
  9. // Bucket 相关 API
  10. type BucketService service
  11. // BucketGetResult ...
  12. type BucketGetResult struct {
  13. XMLName xml.Name `xml:"ListBucketResult"`
  14. Name string
  15. Prefix string `xml:"Prefix,omitempty"`
  16. Marker string `xml:"Marker,omitempty"`
  17. NextMarker string `xml:"NextMarker,omitempty"`
  18. Delimiter string `xml:"Delimiter,omitempty"`
  19. MaxKeys int
  20. IsTruncated bool
  21. Contents []Object `xml:"Contents,omitempty"`
  22. CommonPrefixes []string `xml:"CommonPrefixes>Prefix,omitempty"`
  23. EncodingType string `xml:"Encoding-Type,omitempty"`
  24. }
  25. // BucketGetOptions ...
  26. type BucketGetOptions struct {
  27. Prefix string `url:"prefix,omitempty"`
  28. Delimiter string `url:"delimiter,omitempty"`
  29. EncodingType string `url:"encoding-type,omitempty"`
  30. Marker string `url:"marker,omitempty"`
  31. MaxKeys int `url:"max-keys,omitempty"`
  32. }
  33. // Get Bucket请求等同于 List Object请求,可以列出该Bucket下部分或者所有Object,发起该请求需要拥有Read权限。
  34. //
  35. // https://www.qcloud.com/document/product/436/7734
  36. func (s *BucketService) Get(ctx context.Context, opt *BucketGetOptions) (*BucketGetResult, *Response, error) {
  37. var res BucketGetResult
  38. sendOpt := sendOptions{
  39. baseURL: s.client.BaseURL.BucketURL,
  40. uri: "/",
  41. method: http.MethodGet,
  42. optQuery: opt,
  43. result: &res,
  44. }
  45. resp, err := s.client.send(ctx, &sendOpt)
  46. return &res, resp, err
  47. }
  48. // BucketPutOptions ...
  49. type BucketPutOptions ACLHeaderOptions
  50. // Put Bucket请求可以在指定账号下创建一个Bucket。
  51. //
  52. // https://www.qcloud.com/document/product/436/7738
  53. func (s *BucketService) Put(ctx context.Context, opt *BucketPutOptions) (*Response, error) {
  54. sendOpt := sendOptions{
  55. baseURL: s.client.BaseURL.BucketURL,
  56. uri: "/",
  57. method: http.MethodPut,
  58. optHeader: opt,
  59. }
  60. resp, err := s.client.send(ctx, &sendOpt)
  61. return resp, err
  62. }
  63. // Delete Bucket请求可以在指定账号下删除Bucket,删除之前要求Bucket为空。
  64. //
  65. // https://www.qcloud.com/document/product/436/7732
  66. func (s *BucketService) Delete(ctx context.Context) (*Response, error) {
  67. sendOpt := sendOptions{
  68. baseURL: s.client.BaseURL.BucketURL,
  69. uri: "/",
  70. method: http.MethodDelete,
  71. }
  72. resp, err := s.client.send(ctx, &sendOpt)
  73. return resp, err
  74. }
  75. // Head Bucket请求可以确认是否存在该Bucket,是否有权限访问,Head的权限与Read一致。
  76. //
  77. // 当其存在时,返回 HTTP 状态码200;
  78. // 当无权限时,返回 HTTP 状态码403;
  79. // 当不存在时,返回 HTTP 状态码404。
  80. //
  81. // https://www.qcloud.com/document/product/436/7735
  82. func (s *BucketService) Head(ctx context.Context) (*Response, error) {
  83. sendOpt := sendOptions{
  84. baseURL: s.client.BaseURL.BucketURL,
  85. uri: "/",
  86. method: http.MethodHead,
  87. }
  88. resp, err := s.client.send(ctx, &sendOpt)
  89. return resp, err
  90. }
  91. // Bucket ...
  92. type Bucket struct {
  93. Name string
  94. Region string `xml:"Location,omitempty"`
  95. CreationDate string `xml:",omitempty"`
  96. }