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.

104 lines
3.1 KiB

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