123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package minio
- import (
- "bytes"
- "context"
- "encoding/xml"
- "net/http"
- "github.com/minio/minio-go/v7/pkg/s3utils"
- )
- func (c Client) makeBucket(ctx context.Context, bucketName string, opts MakeBucketOptions) (err error) {
-
- if err := s3utils.CheckValidBucketNameStrict(bucketName); err != nil {
- return err
- }
- err = c.doMakeBucket(ctx, bucketName, opts.Region, opts.ObjectLocking)
- if err != nil && (opts.Region == "" || opts.Region == "us-east-1") {
- if resp, ok := err.(ErrorResponse); ok && resp.Code == "AuthorizationHeaderMalformed" && resp.Region != "" {
- err = c.doMakeBucket(ctx, bucketName, resp.Region, opts.ObjectLocking)
- }
- }
- return err
- }
- func (c Client) doMakeBucket(ctx context.Context, bucketName string, location string, objectLockEnabled bool) (err error) {
- defer func() {
-
- if err == nil {
- c.bucketLocCache.Set(bucketName, location)
- }
- }()
-
- if location == "" {
- location = "us-east-1"
-
-
- if c.region != "" {
- location = c.region
- }
- }
-
- reqMetadata := requestMetadata{
- bucketName: bucketName,
- bucketLocation: location,
- }
- if objectLockEnabled {
- headers := make(http.Header)
- headers.Add("x-amz-bucket-object-lock-enabled", "true")
- reqMetadata.customHeader = headers
- }
-
- if location != "us-east-1" && location != "" {
- createBucketConfig := createBucketConfiguration{}
- createBucketConfig.Location = location
- var createBucketConfigBytes []byte
- createBucketConfigBytes, err = xml.Marshal(createBucketConfig)
- if err != nil {
- return err
- }
- reqMetadata.contentMD5Base64 = sumMD5Base64(createBucketConfigBytes)
- reqMetadata.contentSHA256Hex = sum256Hex(createBucketConfigBytes)
- reqMetadata.contentBody = bytes.NewReader(createBucketConfigBytes)
- reqMetadata.contentLength = int64(len(createBucketConfigBytes))
- }
-
- resp, err := c.executeMethod(ctx, http.MethodPut, reqMetadata)
- defer closeResponse(resp)
- if err != nil {
- return err
- }
- if resp != nil {
- if resp.StatusCode != http.StatusOK {
- return httpRespToErrorResponse(resp, bucketName, "")
- }
- }
-
- return nil
- }
- type MakeBucketOptions struct {
-
- Region string
-
- ObjectLocking bool
- }
- func (c Client) MakeBucket(ctx context.Context, bucketName string, opts MakeBucketOptions) (err error) {
- return c.makeBucket(ctx, bucketName, opts)
- }
|