api-get-acl.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. "net/http"
  21. "net/url"
  22. "github.com/minio/minio-go/v6/pkg/s3utils"
  23. )
  24. // GetBucketAcl - get bucket acl at a given path.
  25. func (c Client) GetBucketAcl(bucketName string) (*AccessControlPolicy, error) {
  26. // Input validation.
  27. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  28. return nil, err
  29. }
  30. bucketAcl, err := c.getBucketAcl(bucketName)
  31. if err != nil {
  32. errResponse := ToErrorResponse(err)
  33. if errResponse.Code == "NoSuchBucketPolicy" {
  34. return nil, nil
  35. }
  36. return nil, err
  37. }
  38. return bucketAcl, nil
  39. }
  40. // Request server for current bucket ACL.
  41. func (c Client) getBucketAcl(bucketName string) (*AccessControlPolicy, error) {
  42. // Get resources properly escaped and lined up before
  43. // using them in http request.
  44. urlValues := make(url.Values)
  45. urlValues.Set("acl", "")
  46. // Execute GET on bucket to list objects.
  47. resp, err := c.executeMethod(context.Background(), "GET", requestMetadata{
  48. bucketName: bucketName,
  49. queryValues: urlValues,
  50. contentSHA256Hex: emptySHA256Hex,
  51. })
  52. defer closeResponse(resp)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if resp != nil {
  57. if resp.StatusCode != http.StatusOK {
  58. return nil, httpRespToErrorResponse(resp, bucketName, "")
  59. }
  60. }
  61. acl := &AccessControlPolicy{}
  62. err = xmlDecoder(resp.Body, acl)
  63. if err != nil {
  64. return nil, err
  65. }
  66. return acl, err
  67. }