api-bucket-versioning.go (4547B)
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 17 package minio 18 19 import ( 20 "bytes" 21 "context" 22 "encoding/xml" 23 "net/http" 24 "net/url" 25 26 "github.com/minio/minio-go/v7/pkg/s3utils" 27 ) 28 29 // SetBucketVersioning sets a bucket versioning configuration 30 func (c *Client) SetBucketVersioning(ctx context.Context, bucketName string, config BucketVersioningConfiguration) error { 31 // Input validation. 32 if err := s3utils.CheckValidBucketName(bucketName); err != nil { 33 return err 34 } 35 36 buf, err := xml.Marshal(config) 37 if err != nil { 38 return err 39 } 40 41 // Get resources properly escaped and lined up before 42 // using them in http request. 43 urlValues := make(url.Values) 44 urlValues.Set("versioning", "") 45 46 reqMetadata := requestMetadata{ 47 bucketName: bucketName, 48 queryValues: urlValues, 49 contentBody: bytes.NewReader(buf), 50 contentLength: int64(len(buf)), 51 contentMD5Base64: sumMD5Base64(buf), 52 contentSHA256Hex: sum256Hex(buf), 53 } 54 55 // Execute PUT to set a bucket versioning. 56 resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata) 57 defer closeResponse(resp) 58 if err != nil { 59 return err 60 } 61 if resp != nil { 62 if resp.StatusCode != http.StatusOK { 63 return httpRespToErrorResponse(resp, bucketName, "") 64 } 65 } 66 return nil 67 } 68 69 // EnableVersioning - enable object versioning in given bucket. 70 func (c *Client) EnableVersioning(ctx context.Context, bucketName string) error { 71 return c.SetBucketVersioning(ctx, bucketName, BucketVersioningConfiguration{Status: "Enabled"}) 72 } 73 74 // SuspendVersioning - suspend object versioning in given bucket. 75 func (c *Client) SuspendVersioning(ctx context.Context, bucketName string) error { 76 return c.SetBucketVersioning(ctx, bucketName, BucketVersioningConfiguration{Status: "Suspended"}) 77 } 78 79 // ExcludedPrefix - holds individual prefixes excluded from being versioned. 80 type ExcludedPrefix struct { 81 Prefix string 82 } 83 84 // BucketVersioningConfiguration is the versioning configuration structure 85 type BucketVersioningConfiguration struct { 86 XMLName xml.Name `xml:"VersioningConfiguration"` 87 Status string `xml:"Status"` 88 MFADelete string `xml:"MfaDelete,omitempty"` 89 // MinIO extension - allows selective, prefix-level versioning exclusion. 90 // Requires versioning to be enabled 91 ExcludedPrefixes []ExcludedPrefix `xml:",omitempty"` 92 ExcludeFolders bool `xml:",omitempty"` 93 } 94 95 // Various supported states 96 const ( 97 Enabled = "Enabled" 98 // Disabled State = "Disabled" only used by MFA Delete not supported yet. 99 Suspended = "Suspended" 100 ) 101 102 // Enabled returns true if bucket versioning is enabled 103 func (b BucketVersioningConfiguration) Enabled() bool { 104 return b.Status == Enabled 105 } 106 107 // Suspended returns true if bucket versioning is suspended 108 func (b BucketVersioningConfiguration) Suspended() bool { 109 return b.Status == Suspended 110 } 111 112 // GetBucketVersioning gets the versioning configuration on 113 // an existing bucket with a context to control cancellations and timeouts. 114 func (c *Client) GetBucketVersioning(ctx context.Context, bucketName string) (BucketVersioningConfiguration, error) { 115 // Input validation. 116 if err := s3utils.CheckValidBucketName(bucketName); err != nil { 117 return BucketVersioningConfiguration{}, err 118 } 119 120 // Get resources properly escaped and lined up before 121 // using them in http request. 122 urlValues := make(url.Values) 123 urlValues.Set("versioning", "") 124 125 // Execute GET on bucket to get the versioning configuration. 126 resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{ 127 bucketName: bucketName, 128 queryValues: urlValues, 129 }) 130 131 defer closeResponse(resp) 132 if err != nil { 133 return BucketVersioningConfiguration{}, err 134 } 135 136 if resp.StatusCode != http.StatusOK { 137 return BucketVersioningConfiguration{}, httpRespToErrorResponse(resp, bucketName, "") 138 } 139 140 versioningConfig := BucketVersioningConfiguration{} 141 if err = xmlDecoder(resp.Body, &versioningConfig); err != nil { 142 return versioningConfig, err 143 } 144 145 return versioningConfig, nil 146 }