github.com/minio/minio-go/v6@v6.0.57/api-bucket-tagging.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  	"bytes"
    21  	"context"
    22  	"encoding/xml"
    23  	"errors"
    24  	"io"
    25  	"io/ioutil"
    26  	"net/http"
    27  	"net/url"
    28  
    29  	"github.com/minio/minio-go/v6/pkg/s3utils"
    30  	"github.com/minio/minio-go/v6/pkg/tags"
    31  )
    32  
    33  // GetBucketTagging gets tagging configuration for a bucket.
    34  func (c Client) GetBucketTagging(bucketName string) (*tags.Tags, error) {
    35  	return c.GetBucketTaggingWithContext(context.Background(), bucketName)
    36  }
    37  
    38  // GetBucketTaggingWithContext gets tagging configuration for a bucket with a context to control cancellations and timeouts.
    39  func (c Client) GetBucketTaggingWithContext(ctx context.Context, bucketName string) (*tags.Tags, error) {
    40  	// Input validation.
    41  	if err := s3utils.CheckValidBucketName(bucketName); err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	// Get resources properly escaped and lined up before
    46  	// using them in http request.
    47  	urlValues := make(url.Values)
    48  	urlValues.Set("tagging", "")
    49  
    50  	// Execute GET on bucket to get tagging configuration.
    51  	resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{
    52  		bucketName:  bucketName,
    53  		queryValues: urlValues,
    54  	})
    55  
    56  	defer closeResponse(resp)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	if resp.StatusCode != http.StatusOK {
    62  		return nil, httpRespToErrorResponse(resp, bucketName, "")
    63  	}
    64  
    65  	defer io.Copy(ioutil.Discard, resp.Body)
    66  	return tags.ParseBucketXML(resp.Body)
    67  }
    68  
    69  // SetBucketTagging sets tagging configuration for a bucket.
    70  func (c Client) SetBucketTagging(bucketName string, tags *tags.Tags) error {
    71  	return c.SetBucketTaggingWithContext(context.Background(), bucketName, tags)
    72  }
    73  
    74  // SetBucketTaggingWithContext sets tagging configuration for a bucket with a context to control cancellations and timeouts.
    75  func (c Client) SetBucketTaggingWithContext(ctx context.Context, bucketName string, tags *tags.Tags) error {
    76  	// Input validation.
    77  	if err := s3utils.CheckValidBucketName(bucketName); err != nil {
    78  		return err
    79  	}
    80  
    81  	if tags == nil {
    82  		return errors.New("nil tags passed")
    83  	}
    84  
    85  	buf, err := xml.Marshal(tags)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	// Get resources properly escaped and lined up before
    91  	// using them in http request.
    92  	urlValues := make(url.Values)
    93  	urlValues.Set("tagging", "")
    94  
    95  	// Content-length is mandatory to set a default encryption configuration
    96  	reqMetadata := requestMetadata{
    97  		bucketName:       bucketName,
    98  		queryValues:      urlValues,
    99  		contentBody:      bytes.NewReader(buf),
   100  		contentLength:    int64(len(buf)),
   101  		contentMD5Base64: sumMD5Base64(buf),
   102  	}
   103  
   104  	// Execute PUT on bucket to put tagging configuration.
   105  	resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata)
   106  	defer closeResponse(resp)
   107  	if err != nil {
   108  		return err
   109  	}
   110  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
   111  		return httpRespToErrorResponse(resp, bucketName, "")
   112  	}
   113  	return nil
   114  }
   115  
   116  // DeleteBucketTagging removes tagging configuration for a bucket.
   117  func (c Client) DeleteBucketTagging(bucketName string) error {
   118  	return c.DeleteBucketTaggingWithContext(context.Background(), bucketName)
   119  }
   120  
   121  // DeleteBucketTaggingWithContext removes tagging configuration for a bucket with a context to control cancellations and timeouts.
   122  func (c Client) DeleteBucketTaggingWithContext(ctx context.Context, bucketName string) error {
   123  	// Input validation.
   124  	if err := s3utils.CheckValidBucketName(bucketName); err != nil {
   125  		return err
   126  	}
   127  
   128  	// Get resources properly escaped and lined up before
   129  	// using them in http request.
   130  	urlValues := make(url.Values)
   131  	urlValues.Set("tagging", "")
   132  
   133  	// Execute DELETE on bucket to remove tagging configuration.
   134  	resp, err := c.executeMethod(ctx, http.MethodDelete, requestMetadata{
   135  		bucketName:       bucketName,
   136  		queryValues:      urlValues,
   137  		contentSHA256Hex: emptySHA256Hex,
   138  	})
   139  	defer closeResponse(resp)
   140  	if err != nil {
   141  		return err
   142  	}
   143  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
   144  		return httpRespToErrorResponse(resp, bucketName, "")
   145  	}
   146  	return nil
   147  }