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.

api-bucket-versioning.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2020 MinIO, Inc.
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package minio
  17. import (
  18. "bytes"
  19. "context"
  20. "encoding/xml"
  21. "net/http"
  22. "net/url"
  23. "github.com/minio/minio-go/v7/pkg/s3utils"
  24. )
  25. // SetBucketVersioning sets a bucket versioning configuration
  26. func (c Client) SetBucketVersioning(ctx context.Context, bucketName string, config BucketVersioningConfiguration) error {
  27. // Input validation.
  28. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  29. return err
  30. }
  31. buf, err := xml.Marshal(config)
  32. if err != nil {
  33. return err
  34. }
  35. // Get resources properly escaped and lined up before
  36. // using them in http request.
  37. urlValues := make(url.Values)
  38. urlValues.Set("versioning", "")
  39. reqMetadata := requestMetadata{
  40. bucketName: bucketName,
  41. queryValues: urlValues,
  42. contentBody: bytes.NewReader(buf),
  43. contentLength: int64(len(buf)),
  44. contentMD5Base64: sumMD5Base64(buf),
  45. contentSHA256Hex: sum256Hex(buf),
  46. }
  47. // Execute PUT to set a bucket versioning.
  48. resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata)
  49. defer closeResponse(resp)
  50. if err != nil {
  51. return err
  52. }
  53. if resp != nil {
  54. if resp.StatusCode != http.StatusOK {
  55. return httpRespToErrorResponse(resp, bucketName, "")
  56. }
  57. }
  58. return nil
  59. }
  60. // EnableVersioning - enable object versioning in given bucket.
  61. func (c Client) EnableVersioning(ctx context.Context, bucketName string) error {
  62. return c.SetBucketVersioning(ctx, bucketName, BucketVersioningConfiguration{Status: "Enabled"})
  63. }
  64. // SuspendVersioning - suspend object versioning in given bucket.
  65. func (c Client) SuspendVersioning(ctx context.Context, bucketName string) error {
  66. return c.SetBucketVersioning(ctx, bucketName, BucketVersioningConfiguration{Status: "Suspended"})
  67. }
  68. // BucketVersioningConfiguration is the versioning configuration structure
  69. type BucketVersioningConfiguration struct {
  70. XMLName xml.Name `xml:"VersioningConfiguration"`
  71. Status string `xml:"Status"`
  72. MFADelete string `xml:"MfaDelete,omitempty"`
  73. }
  74. // Various supported states
  75. const (
  76. Enabled = "Enabled"
  77. // Disabled State = "Disabled" only used by MFA Delete not supported yet.
  78. Suspended = "Suspended"
  79. )
  80. // Enabled returns true if bucket versioning is enabled
  81. func (b BucketVersioningConfiguration) Enabled() bool {
  82. return b.Status == Enabled
  83. }
  84. // Suspended returns true if bucket versioning is suspended
  85. func (b BucketVersioningConfiguration) Suspended() bool {
  86. return b.Status == Suspended
  87. }
  88. // GetBucketVersioning gets the versioning configuration on
  89. // an existing bucket with a context to control cancellations and timeouts.
  90. func (c Client) GetBucketVersioning(ctx context.Context, bucketName string) (BucketVersioningConfiguration, error) {
  91. // Input validation.
  92. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  93. return BucketVersioningConfiguration{}, err
  94. }
  95. // Get resources properly escaped and lined up before
  96. // using them in http request.
  97. urlValues := make(url.Values)
  98. urlValues.Set("versioning", "")
  99. // Execute GET on bucket to get the versioning configuration.
  100. resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{
  101. bucketName: bucketName,
  102. queryValues: urlValues,
  103. })
  104. defer closeResponse(resp)
  105. if err != nil {
  106. return BucketVersioningConfiguration{}, err
  107. }
  108. if resp.StatusCode != http.StatusOK {
  109. return BucketVersioningConfiguration{}, httpRespToErrorResponse(resp, bucketName, "")
  110. }
  111. versioningConfig := BucketVersioningConfiguration{}
  112. if err = xmlDecoder(resp.Body, &versioningConfig); err != nil {
  113. return versioningConfig, err
  114. }
  115. return versioningConfig, nil
  116. }