github.com/minio/minio-go/v6@v6.0.57/api-get-bucket-versioning.go (about)

     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  	"context"
    21  	"encoding/xml"
    22  	"io/ioutil"
    23  	"net/http"
    24  	"net/url"
    25  
    26  	"github.com/minio/minio-go/v6/pkg/s3utils"
    27  )
    28  
    29  // BucketVersioningConfiguration is the versioning configuration structure
    30  type BucketVersioningConfiguration struct {
    31  	XMLName   xml.Name `xml:"VersioningConfiguration"`
    32  	Status    string   `xml:"Status"`
    33  	MfaDelete string   `xml:"MfaDelete,omitempty"`
    34  }
    35  
    36  // GetBucketVersioning - get versioning configuration for a bucket.
    37  func (c Client) GetBucketVersioning(bucketName string) (BucketVersioningConfiguration, error) {
    38  	return c.GetBucketVersioningWithContext(context.Background(), bucketName)
    39  }
    40  
    41  // GetBucketVersioningWithContext gets the versioning configuration on an existing bucket with a context to control cancellations and timeouts.
    42  func (c Client) GetBucketVersioningWithContext(ctx context.Context, bucketName string) (BucketVersioningConfiguration, error) {
    43  	// Input validation.
    44  	if err := s3utils.CheckValidBucketName(bucketName); err != nil {
    45  		return BucketVersioningConfiguration{}, err
    46  	}
    47  
    48  	// Get resources properly escaped and lined up before
    49  	// using them in http request.
    50  	urlValues := make(url.Values)
    51  	urlValues.Set("versioning", "")
    52  
    53  	// Execute GET on bucket to get the versioning configuration.
    54  	resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{
    55  		bucketName:  bucketName,
    56  		queryValues: urlValues,
    57  	})
    58  
    59  	defer closeResponse(resp)
    60  	if err != nil {
    61  		return BucketVersioningConfiguration{}, err
    62  	}
    63  
    64  	if resp.StatusCode != http.StatusOK {
    65  		return BucketVersioningConfiguration{}, httpRespToErrorResponse(resp, bucketName, "")
    66  	}
    67  
    68  	bucketVersioningBuf, err := ioutil.ReadAll(resp.Body)
    69  	if err != nil {
    70  		return BucketVersioningConfiguration{}, err
    71  	}
    72  
    73  	versioningConfig := BucketVersioningConfiguration{}
    74  	if err := xml.Unmarshal(bucketVersioningBuf, &versioningConfig); err != nil {
    75  		return BucketVersioningConfiguration{}, err
    76  	}
    77  	return versioningConfig, nil
    78  }