api-stat.go (3781B)
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 18 package minio 19 20 import ( 21 "context" 22 "net/http" 23 24 "github.com/minio/minio-go/v7/pkg/s3utils" 25 ) 26 27 // BucketExists verifies if bucket exists and you have permission to access it. Allows for a Context to 28 // control cancellations and timeouts. 29 func (c *Client) BucketExists(ctx context.Context, bucketName string) (bool, error) { 30 // Input validation. 31 if err := s3utils.CheckValidBucketName(bucketName); err != nil { 32 return false, err 33 } 34 35 // Execute HEAD on bucketName. 36 resp, err := c.executeMethod(ctx, http.MethodHead, requestMetadata{ 37 bucketName: bucketName, 38 contentSHA256Hex: emptySHA256Hex, 39 }) 40 defer closeResponse(resp) 41 if err != nil { 42 if ToErrorResponse(err).Code == "NoSuchBucket" { 43 return false, nil 44 } 45 return false, err 46 } 47 if resp != nil { 48 resperr := httpRespToErrorResponse(resp, bucketName, "") 49 if ToErrorResponse(resperr).Code == "NoSuchBucket" { 50 return false, nil 51 } 52 if resp.StatusCode != http.StatusOK { 53 return false, httpRespToErrorResponse(resp, bucketName, "") 54 } 55 } 56 return true, nil 57 } 58 59 // StatObject verifies if object exists, you have permission to access it 60 // and returns information about the object. 61 func (c *Client) StatObject(ctx context.Context, bucketName, objectName string, opts StatObjectOptions) (ObjectInfo, error) { 62 // Input validation. 63 if err := s3utils.CheckValidBucketName(bucketName); err != nil { 64 return ObjectInfo{}, err 65 } 66 if err := s3utils.CheckValidObjectName(objectName); err != nil { 67 return ObjectInfo{}, err 68 } 69 headers := opts.Header() 70 if opts.Internal.ReplicationDeleteMarker { 71 headers.Set(minIOBucketReplicationDeleteMarker, "true") 72 } 73 if opts.Internal.IsReplicationReadyForDeleteMarker { 74 headers.Set(isMinioTgtReplicationReady, "true") 75 } 76 77 // Execute HEAD on objectName. 78 resp, err := c.executeMethod(ctx, http.MethodHead, requestMetadata{ 79 bucketName: bucketName, 80 objectName: objectName, 81 queryValues: opts.toQueryValues(), 82 contentSHA256Hex: emptySHA256Hex, 83 customHeader: headers, 84 }) 85 defer closeResponse(resp) 86 if err != nil { 87 return ObjectInfo{}, err 88 } 89 90 if resp != nil { 91 deleteMarker := resp.Header.Get(amzDeleteMarker) == "true" 92 replicationReady := resp.Header.Get(minioTgtReplicationReady) == "true" 93 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { 94 if resp.StatusCode == http.StatusMethodNotAllowed && opts.VersionID != "" && deleteMarker { 95 errResp := ErrorResponse{ 96 StatusCode: resp.StatusCode, 97 Code: "MethodNotAllowed", 98 Message: "The specified method is not allowed against this resource.", 99 BucketName: bucketName, 100 Key: objectName, 101 } 102 return ObjectInfo{ 103 VersionID: resp.Header.Get(amzVersionID), 104 IsDeleteMarker: deleteMarker, 105 }, errResp 106 } 107 return ObjectInfo{ 108 VersionID: resp.Header.Get(amzVersionID), 109 IsDeleteMarker: deleteMarker, 110 ReplicationReady: replicationReady, // whether delete marker can be replicated 111 }, httpRespToErrorResponse(resp, bucketName, objectName) 112 } 113 } 114 115 return ToObjectInfo(bucketName, objectName, resp.Header) 116 }