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.

177 lines
6.8 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:"EncodingType,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 struct {
  48. XCosACL string `header:"x-cos-acl,omitempty" url:"-" xml:"-"`
  49. XCosGrantRead string `header:"x-cos-grant-read,omitempty" url:"-" xml:"-"`
  50. XCosGrantWrite string `header:"x-cos-grant-write,omitempty" url:"-" xml:"-"`
  51. XCosGrantFullControl string `header:"x-cos-grant-full-control,omitempty" url:"-" xml:"-"`
  52. XCosGrantReadACP string `header:"x-cos-grant-read-acp,omitempty" url:"-" xml:"-"`
  53. XCosGrantWriteACP string `header:"x-cos-grant-write-acp,omitempty" url:"-" xml:"-"`
  54. CreateBucketConfiguration *CreateBucketConfiguration `header:"-" url:"-" xml:"-"`
  55. }
  56. type CreateBucketConfiguration struct {
  57. XMLName xml.Name `xml:"CreateBucketConfiguration"`
  58. BucketAZConfig string `xml:"BucketAZConfig,omitempty"`
  59. }
  60. // Put Bucket请求可以在指定账号下创建一个Bucket。
  61. //
  62. // https://www.qcloud.com/document/product/436/7738
  63. func (s *BucketService) Put(ctx context.Context, opt *BucketPutOptions) (*Response, error) {
  64. sendOpt := sendOptions{
  65. baseURL: s.client.BaseURL.BucketURL,
  66. uri: "/",
  67. method: http.MethodPut,
  68. optHeader: opt,
  69. }
  70. if opt != nil && opt.CreateBucketConfiguration != nil {
  71. sendOpt.body = opt.CreateBucketConfiguration
  72. }
  73. resp, err := s.client.send(ctx, &sendOpt)
  74. return resp, err
  75. }
  76. // Delete Bucket请求可以在指定账号下删除Bucket,删除之前要求Bucket为空。
  77. //
  78. // https://www.qcloud.com/document/product/436/7732
  79. func (s *BucketService) Delete(ctx context.Context) (*Response, error) {
  80. sendOpt := sendOptions{
  81. baseURL: s.client.BaseURL.BucketURL,
  82. uri: "/",
  83. method: http.MethodDelete,
  84. }
  85. resp, err := s.client.send(ctx, &sendOpt)
  86. return resp, err
  87. }
  88. // Head Bucket请求可以确认是否存在该Bucket,是否有权限访问,Head的权限与Read一致。
  89. //
  90. // 当其存在时,返回 HTTP 状态码200;
  91. // 当无权限时,返回 HTTP 状态码403;
  92. // 当不存在时,返回 HTTP 状态码404。
  93. //
  94. // https://www.qcloud.com/document/product/436/7735
  95. func (s *BucketService) Head(ctx context.Context) (*Response, error) {
  96. sendOpt := sendOptions{
  97. baseURL: s.client.BaseURL.BucketURL,
  98. uri: "/",
  99. method: http.MethodHead,
  100. }
  101. resp, err := s.client.send(ctx, &sendOpt)
  102. return resp, err
  103. }
  104. // Bucket is the meta info of Bucket
  105. type Bucket struct {
  106. Name string
  107. Region string `xml:"Location,omitempty"`
  108. CreationDate string `xml:",omitempty"`
  109. }
  110. type BucketGetObjectVersionsOptions struct {
  111. Prefix string `url:"prefix,omitempty"`
  112. Delimiter string `url:"delimiter,omitempty"`
  113. EncodingType string `url:"encoding-type,omitempty"`
  114. KeyMarker string `url:"key-marker,omitempty"`
  115. VersionIdMarker string `url:"version-id-marker,omitempty"`
  116. MaxKeys int `url:"max-keys,omitempty"`
  117. }
  118. type BucketGetObjectVersionsResult struct {
  119. XMLName xml.Name `xml:"ListVersionsResult"`
  120. Name string `xml:"Name,omitempty"`
  121. EncodingType string `xml:"EncodingType,omitempty"`
  122. Prefix string `xml:"Prefix,omitempty"`
  123. KeyMarker string `xml:"KeyMarker,omitempty"`
  124. VersionIdMarker string `xml:"VersionIdMarker,omitempty"`
  125. MaxKeys int `xml:"MaxKeys,omitempty"`
  126. Delimiter string `xml:"Delimiter,omitempty"`
  127. IsTruncated bool `xml:"IsTruncated,omitempty"`
  128. NextKeyMarker string `xml:"NextKeyMarker,omitempty"`
  129. NextVersionIdMarker string `xml:"NextVersionIdMarker,omitempty"`
  130. CommonPrefixes []string `xml:"CommonPrefixes>Prefix,omitempty"`
  131. Version []ListVersionsResultVersion `xml:"Version,omitempty"`
  132. DeleteMarker []ListVersionsResultDeleteMarker `xml:"DeleteMarker,omitempty"`
  133. }
  134. type ListVersionsResultVersion struct {
  135. Key string `xml:"Key,omitempty"`
  136. VersionId string `xml:"VersionId,omitempty"`
  137. IsLatest bool `xml:"IsLatest,omitempty"`
  138. LastModified string `xml:"LastModified,omitempty"`
  139. ETag string `xml:"ETag,omitempty"`
  140. Size int `xml:"Size,omitempty"`
  141. StorageClass string `xml:"StorageClass,omitempty"`
  142. Owner *Owner `xml:"Owner,omitempty"`
  143. }
  144. type ListVersionsResultDeleteMarker struct {
  145. Key string `xml:"Key,omitempty"`
  146. VersionId string `xml:"VersionId,omitempty"`
  147. IsLatest bool `xml:"IsLatest,omitempty"`
  148. LastModified string `xml:"LastModified,omitempty"`
  149. Owner *Owner `xml:"Owner,omitempty"`
  150. }
  151. func (s *BucketService) GetObjectVersions(ctx context.Context, opt *BucketGetObjectVersionsOptions) (*BucketGetObjectVersionsResult, *Response, error) {
  152. var res BucketGetObjectVersionsResult
  153. sendOpt := sendOptions{
  154. baseURL: s.client.BaseURL.BucketURL,
  155. uri: "/?versions",
  156. method: http.MethodGet,
  157. optQuery: opt,
  158. result: &res,
  159. }
  160. resp, err := s.client.send(ctx, &sendOpt)
  161. return &res, resp, err
  162. }