api-put-bucket.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 minio
  18. import (
  19. "bytes"
  20. "context"
  21. "encoding/json"
  22. "encoding/xml"
  23. "fmt"
  24. "net/http"
  25. "net/url"
  26. "github.com/minio/minio-go/pkg/policy"
  27. "github.com/minio/minio-go/pkg/s3utils"
  28. )
  29. /// Bucket operations
  30. // MakeBucket creates a new bucket with bucketName.
  31. //
  32. // Location is an optional argument, by default all buckets are
  33. // created in US Standard Region.
  34. //
  35. // For Amazon S3 for more supported regions - http://docs.aws.amazon.com/general/latest/gr/rande.html
  36. // For Google Cloud Storage for more supported regions - https://cloud.google.com/storage/docs/bucket-locations
  37. func (c Client) MakeBucket(bucketName string, location string) (err error) {
  38. defer func() {
  39. // Save the location into cache on a successful makeBucket response.
  40. if err == nil {
  41. c.bucketLocCache.Set(bucketName, location)
  42. }
  43. }()
  44. // Validate the input arguments.
  45. if err := s3utils.CheckValidBucketNameStrict(bucketName); err != nil {
  46. return err
  47. }
  48. // If location is empty, treat is a default region 'us-east-1'.
  49. if location == "" {
  50. location = "us-east-1"
  51. // For custom region clients, default
  52. // to custom region instead not 'us-east-1'.
  53. if c.region != "" {
  54. location = c.region
  55. }
  56. }
  57. // PUT bucket request metadata.
  58. reqMetadata := requestMetadata{
  59. bucketName: bucketName,
  60. bucketLocation: location,
  61. }
  62. // If location is not 'us-east-1' create bucket location config.
  63. if location != "us-east-1" && location != "" {
  64. createBucketConfig := createBucketConfiguration{}
  65. createBucketConfig.Location = location
  66. var createBucketConfigBytes []byte
  67. createBucketConfigBytes, err = xml.Marshal(createBucketConfig)
  68. if err != nil {
  69. return err
  70. }
  71. reqMetadata.contentMD5Base64 = sumMD5Base64(createBucketConfigBytes)
  72. reqMetadata.contentSHA256Hex = sum256Hex(createBucketConfigBytes)
  73. reqMetadata.contentBody = bytes.NewReader(createBucketConfigBytes)
  74. reqMetadata.contentLength = int64(len(createBucketConfigBytes))
  75. }
  76. // Execute PUT to create a new bucket.
  77. resp, err := c.executeMethod(context.Background(), "PUT", reqMetadata)
  78. defer closeResponse(resp)
  79. if err != nil {
  80. return err
  81. }
  82. if resp != nil {
  83. if resp.StatusCode != http.StatusOK {
  84. return httpRespToErrorResponse(resp, bucketName, "")
  85. }
  86. }
  87. // Success.
  88. return nil
  89. }
  90. // SetBucketPolicy set the access permissions on an existing bucket.
  91. //
  92. // For example
  93. //
  94. // none - owner gets full access [default].
  95. // readonly - anonymous get access for everyone at a given object prefix.
  96. // readwrite - anonymous list/put/delete access to a given object prefix.
  97. // writeonly - anonymous put/delete access to a given object prefix.
  98. func (c Client) SetBucketPolicy(bucketName string, objectPrefix string, bucketPolicy policy.BucketPolicy) error {
  99. // Input validation.
  100. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  101. return err
  102. }
  103. if err := s3utils.CheckValidObjectNamePrefix(objectPrefix); err != nil {
  104. return err
  105. }
  106. if !bucketPolicy.IsValidBucketPolicy() {
  107. return ErrInvalidArgument(fmt.Sprintf("Invalid bucket policy provided. %s", bucketPolicy))
  108. }
  109. policyInfo, err := c.getBucketPolicy(bucketName)
  110. errResponse := ToErrorResponse(err)
  111. if err != nil && errResponse.Code != "NoSuchBucketPolicy" {
  112. return err
  113. }
  114. if bucketPolicy == policy.BucketPolicyNone && policyInfo.Statements == nil {
  115. // As the request is for removing policy and the bucket
  116. // has empty policy statements, just return success.
  117. return nil
  118. }
  119. policyInfo.Statements = policy.SetPolicy(policyInfo.Statements, bucketPolicy, bucketName, objectPrefix)
  120. // Save the updated policies.
  121. return c.putBucketPolicy(bucketName, policyInfo)
  122. }
  123. // Saves a new bucket policy.
  124. func (c Client) putBucketPolicy(bucketName string, policyInfo policy.BucketAccessPolicy) error {
  125. // Input validation.
  126. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  127. return err
  128. }
  129. // If there are no policy statements, we should remove entire policy.
  130. if len(policyInfo.Statements) == 0 {
  131. return c.removeBucketPolicy(bucketName)
  132. }
  133. // Get resources properly escaped and lined up before
  134. // using them in http request.
  135. urlValues := make(url.Values)
  136. urlValues.Set("policy", "")
  137. policyBytes, err := json.Marshal(&policyInfo)
  138. if err != nil {
  139. return err
  140. }
  141. policyBuffer := bytes.NewReader(policyBytes)
  142. reqMetadata := requestMetadata{
  143. bucketName: bucketName,
  144. queryValues: urlValues,
  145. contentBody: policyBuffer,
  146. contentLength: int64(len(policyBytes)),
  147. contentMD5Base64: sumMD5Base64(policyBytes),
  148. contentSHA256Hex: sum256Hex(policyBytes),
  149. }
  150. // Execute PUT to upload a new bucket policy.
  151. resp, err := c.executeMethod(context.Background(), "PUT", reqMetadata)
  152. defer closeResponse(resp)
  153. if err != nil {
  154. return err
  155. }
  156. if resp != nil {
  157. if resp.StatusCode != http.StatusNoContent {
  158. return httpRespToErrorResponse(resp, bucketName, "")
  159. }
  160. }
  161. return nil
  162. }
  163. // Removes all policies on a bucket.
  164. func (c Client) removeBucketPolicy(bucketName string) error {
  165. // Input validation.
  166. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  167. return err
  168. }
  169. // Get resources properly escaped and lined up before
  170. // using them in http request.
  171. urlValues := make(url.Values)
  172. urlValues.Set("policy", "")
  173. // Execute DELETE on objectName.
  174. resp, err := c.executeMethod(context.Background(), "DELETE", requestMetadata{
  175. bucketName: bucketName,
  176. queryValues: urlValues,
  177. contentSHA256Hex: emptySHA256Hex,
  178. })
  179. defer closeResponse(resp)
  180. if err != nil {
  181. return err
  182. }
  183. return nil
  184. }
  185. // SetBucketNotification saves a new bucket notification.
  186. func (c Client) SetBucketNotification(bucketName string, bucketNotification BucketNotification) error {
  187. // Input validation.
  188. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  189. return err
  190. }
  191. // Get resources properly escaped and lined up before
  192. // using them in http request.
  193. urlValues := make(url.Values)
  194. urlValues.Set("notification", "")
  195. notifBytes, err := xml.Marshal(bucketNotification)
  196. if err != nil {
  197. return err
  198. }
  199. notifBuffer := bytes.NewReader(notifBytes)
  200. reqMetadata := requestMetadata{
  201. bucketName: bucketName,
  202. queryValues: urlValues,
  203. contentBody: notifBuffer,
  204. contentLength: int64(len(notifBytes)),
  205. contentMD5Base64: sumMD5Base64(notifBytes),
  206. contentSHA256Hex: sum256Hex(notifBytes),
  207. }
  208. // Execute PUT to upload a new bucket notification.
  209. resp, err := c.executeMethod(context.Background(), "PUT", reqMetadata)
  210. defer closeResponse(resp)
  211. if err != nil {
  212. return err
  213. }
  214. if resp != nil {
  215. if resp.StatusCode != http.StatusOK {
  216. return httpRespToErrorResponse(resp, bucketName, "")
  217. }
  218. }
  219. return nil
  220. }
  221. // RemoveAllBucketNotification - Remove bucket notification clears all previously specified config
  222. func (c Client) RemoveAllBucketNotification(bucketName string) error {
  223. return c.SetBucketNotification(bucketName, BucketNotification{})
  224. }