api-get-lifecycle.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2017 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package s3cli
  18. import (
  19. "context"
  20. "io/ioutil"
  21. "net/http"
  22. "net/url"
  23. "github.com/minio/minio-go/v6/pkg/s3utils"
  24. )
  25. // GetBucketLifecycle - get bucket lifecycle.
  26. func (c Client) GetBucketLifecycle(bucketName string) (string, error) {
  27. // Input validation.
  28. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  29. return "", err
  30. }
  31. bucketLifecycle, err := c.getBucketLifecycle(bucketName)
  32. if err != nil {
  33. errResponse := ToErrorResponse(err)
  34. if errResponse.Code == "NoSuchLifecycleConfiguration" {
  35. return "", nil
  36. }
  37. return "", err
  38. }
  39. return bucketLifecycle, nil
  40. }
  41. // Request server for current bucket lifecycle.
  42. func (c Client) getBucketLifecycle(bucketName string) (string, error) {
  43. // Get resources properly escaped and lined up before
  44. // using them in http request.
  45. urlValues := make(url.Values)
  46. urlValues.Set("lifecycle", "")
  47. // Execute GET on bucket to get lifecycle.
  48. resp, err := c.executeMethod(context.Background(), "GET", requestMetadata{
  49. bucketName: bucketName,
  50. queryValues: urlValues,
  51. })
  52. defer closeResponse(resp)
  53. if err != nil {
  54. return "", err
  55. }
  56. if resp != nil {
  57. if resp.StatusCode != http.StatusOK {
  58. return "", httpRespToErrorResponse(resp, bucketName, "")
  59. }
  60. }
  61. bucketLifecycleBuf, err := ioutil.ReadAll(resp.Body)
  62. if err != nil {
  63. return "", err
  64. }
  65. lifecycle := string(bucketLifecycleBuf)
  66. return lifecycle, err
  67. }