api-put-bucket.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2020 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/xml"
  22. "net/http"
  23. "github.com/minio/minio-go/v7/pkg/s3utils"
  24. )
  25. /// Bucket operations
  26. func (c Client) makeBucket(ctx context.Context, bucketName string, opts MakeBucketOptions) (err error) {
  27. // Validate the input arguments.
  28. if err := s3utils.CheckValidBucketNameStrict(bucketName); err != nil {
  29. return err
  30. }
  31. err = c.doMakeBucket(ctx, bucketName, opts.Region, opts.ObjectLocking)
  32. if err != nil && (opts.Region == "" || opts.Region == "us-east-1") {
  33. if resp, ok := err.(ErrorResponse); ok && resp.Code == "AuthorizationHeaderMalformed" && resp.Region != "" {
  34. err = c.doMakeBucket(ctx, bucketName, resp.Region, opts.ObjectLocking)
  35. }
  36. }
  37. return err
  38. }
  39. func (c Client) doMakeBucket(ctx context.Context, bucketName string, location string, objectLockEnabled bool) (err error) {
  40. defer func() {
  41. // Save the location into cache on a successful makeBucket response.
  42. if err == nil {
  43. c.bucketLocCache.Set(bucketName, location)
  44. }
  45. }()
  46. // If location is empty, treat is a default region 'us-east-1'.
  47. if location == "" {
  48. location = "us-east-1"
  49. // For custom region clients, default
  50. // to custom region instead not 'us-east-1'.
  51. if c.region != "" {
  52. location = c.region
  53. }
  54. }
  55. // PUT bucket request metadata.
  56. reqMetadata := requestMetadata{
  57. bucketName: bucketName,
  58. bucketLocation: location,
  59. }
  60. if objectLockEnabled {
  61. headers := make(http.Header)
  62. headers.Add("x-amz-bucket-object-lock-enabled", "true")
  63. reqMetadata.customHeader = headers
  64. }
  65. // If location is not 'us-east-1' create bucket location config.
  66. if location != "us-east-1" && location != "" {
  67. createBucketConfig := createBucketConfiguration{}
  68. createBucketConfig.Location = location
  69. var createBucketConfigBytes []byte
  70. createBucketConfigBytes, err = xml.Marshal(createBucketConfig)
  71. if err != nil {
  72. return err
  73. }
  74. reqMetadata.contentMD5Base64 = sumMD5Base64(createBucketConfigBytes)
  75. reqMetadata.contentSHA256Hex = sum256Hex(createBucketConfigBytes)
  76. reqMetadata.contentBody = bytes.NewReader(createBucketConfigBytes)
  77. reqMetadata.contentLength = int64(len(createBucketConfigBytes))
  78. }
  79. // Execute PUT to create a new bucket.
  80. resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata)
  81. defer closeResponse(resp)
  82. if err != nil {
  83. return err
  84. }
  85. if resp != nil {
  86. if resp.StatusCode != http.StatusOK {
  87. return httpRespToErrorResponse(resp, bucketName, "")
  88. }
  89. }
  90. // Success.
  91. return nil
  92. }
  93. // MakeBucketOptions holds all options to tweak bucket creation
  94. type MakeBucketOptions struct {
  95. // Bucket location
  96. Region string
  97. // Enable object locking
  98. ObjectLocking bool
  99. }
  100. // MakeBucket creates a new bucket with bucketName with a context to control cancellations and timeouts.
  101. //
  102. // Location is an optional argument, by default all buckets are
  103. // created in US Standard Region.
  104. //
  105. // For Amazon S3 for more supported regions - http://docs.aws.amazon.com/general/latest/gr/rande.html
  106. // For Google Cloud Storage for more supported regions - https://cloud.google.com/storage/docs/bucket-locations
  107. func (c Client) MakeBucket(ctx context.Context, bucketName string, opts MakeBucketOptions) (err error) {
  108. return c.makeBucket(ctx, bucketName, opts)
  109. }