2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-02 09:58:25 +00:00
restic/vendor/github.com/minio/minio-go/api-get-policy.go

79 lines
2.0 KiB
Go
Raw Normal View History

2017-07-23 12:24:45 +00:00
/*
2017-12-08 19:45:59 +00:00
* Minio Go Library for Amazon S3 Compatible Cloud Storage
* Copyright 2015-2017 Minio, Inc.
2017-07-23 12:24:45 +00:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package minio
import (
2017-12-08 19:45:59 +00:00
"context"
2017-07-23 12:24:45 +00:00
"io/ioutil"
"net/http"
"net/url"
"github.com/minio/minio-go/pkg/s3utils"
)
// GetBucketPolicy - get bucket policy at a given path.
func (c Client) GetBucketPolicy(bucketName string) (string, error) {
2017-07-23 12:24:45 +00:00
// Input validation.
if err := s3utils.CheckValidBucketName(bucketName); err != nil {
return "", err
2017-07-23 12:24:45 +00:00
}
bucketPolicy, err := c.getBucketPolicy(bucketName)
2017-07-23 12:24:45 +00:00
if err != nil {
errResponse := ToErrorResponse(err)
if errResponse.Code == "NoSuchBucketPolicy" {
return "", nil
2017-07-23 12:24:45 +00:00
}
return "", err
2017-07-23 12:24:45 +00:00
}
return bucketPolicy, nil
2017-07-23 12:24:45 +00:00
}
// Request server for current bucket policy.
func (c Client) getBucketPolicy(bucketName string) (string, error) {
2017-07-23 12:24:45 +00:00
// Get resources properly escaped and lined up before
// using them in http request.
urlValues := make(url.Values)
urlValues.Set("policy", "")
// Execute GET on bucket to list objects.
2017-12-08 19:45:59 +00:00
resp, err := c.executeMethod(context.Background(), "GET", requestMetadata{
bucketName: bucketName,
queryValues: urlValues,
contentSHA256Hex: emptySHA256Hex,
2017-07-23 12:24:45 +00:00
})
defer closeResponse(resp)
if err != nil {
return "", err
2017-07-23 12:24:45 +00:00
}
if resp != nil {
if resp.StatusCode != http.StatusOK {
return "", httpRespToErrorResponse(resp, bucketName, "")
2017-07-23 12:24:45 +00:00
}
}
bucketPolicyBuf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
2017-07-23 12:24:45 +00:00
}
policy := string(bucketPolicyBuf)
2017-07-23 12:24:45 +00:00
return policy, err
}