|
| 1 | +/* |
| 2 | + * MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 3 | + * Copyright 2020 MinIO, Inc. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package minio |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/xml" |
| 22 | + "errors" |
| 23 | + "net/http" |
| 24 | + "net/url" |
| 25 | + "strconv" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/minio/minio-go/v7/pkg/encrypt" |
| 29 | + "github.com/minio/minio-go/v7/pkg/s3utils" |
| 30 | +) |
| 31 | + |
| 32 | +// ObjectAttributesOptions are options used for the GetObjectAttributes API |
| 33 | +// |
| 34 | +// - MaxParts |
| 35 | +// How many parts the caller wants to be returned (default: 1000) |
| 36 | +// |
| 37 | +// - VersionID |
| 38 | +// The object version you want to attributes for |
| 39 | +// |
| 40 | +// - PartNumberMarker |
| 41 | +// the listing will start AFTER the part matching PartNumberMarker |
| 42 | +// |
| 43 | +// - ServerSideEncryption |
| 44 | +// The server-side encryption algorithm used when storing this object in Minio |
| 45 | +type ObjectAttributesOptions struct { |
| 46 | + MaxParts int |
| 47 | + VersionID string |
| 48 | + PartNumberMarker int |
| 49 | + ServerSideEncryption encrypt.ServerSide |
| 50 | +} |
| 51 | + |
| 52 | +// ObjectAttributes is the response object returned by the GetObjectAttributes API |
| 53 | +// |
| 54 | +// - VersionID |
| 55 | +// The object version |
| 56 | +// |
| 57 | +// - LastModified |
| 58 | +// The last time the object was modified |
| 59 | +// |
| 60 | +// - ObjectAttributesResponse |
| 61 | +// Contains more information about the object |
| 62 | +type ObjectAttributes struct { |
| 63 | + VersionID string |
| 64 | + LastModified time.Time |
| 65 | + ObjectAttributesResponse |
| 66 | +} |
| 67 | + |
| 68 | +// ObjectAttributesResponse contains details returned by the GetObjectAttributes API |
| 69 | +// |
| 70 | +// Noteworthy fields: |
| 71 | +// |
| 72 | +// - ObjectParts.PartsCount |
| 73 | +// Contains the total part count for the object (not the current response) |
| 74 | +// |
| 75 | +// - ObjectParts.PartNumberMarker |
| 76 | +// Pagination of parts will begin at (but not include) PartNumberMarker |
| 77 | +// |
| 78 | +// - ObjectParts.NextPartNumberMarket |
| 79 | +// The next PartNumberMarker to be used in order to continue pagination |
| 80 | +// |
| 81 | +// - ObjectParts.IsTruncated |
| 82 | +// Indicates if the last part is included in the request (does not check if parts are missing from the start of the list, ONLY the end) |
| 83 | +// |
| 84 | +// - ObjectParts.MaxParts |
| 85 | +// Reflects the MaxParts used by the caller or the default MaxParts value of the API |
| 86 | +type ObjectAttributesResponse struct { |
| 87 | + ETag string `xml:",omitempty"` |
| 88 | + StorageClass string |
| 89 | + ObjectSize int |
| 90 | + Checksum struct { |
| 91 | + ChecksumCRC32 string `xml:",omitempty"` |
| 92 | + ChecksumCRC32C string `xml:",omitempty"` |
| 93 | + ChecksumSHA1 string `xml:",omitempty"` |
| 94 | + ChecksumSHA256 string `xml:",omitempty"` |
| 95 | + } |
| 96 | + ObjectParts struct { |
| 97 | + PartsCount int |
| 98 | + PartNumberMarker int |
| 99 | + NextPartNumberMarker int |
| 100 | + MaxParts int |
| 101 | + IsTruncated bool |
| 102 | + Parts []*ObjectAttributePart `xml:"Part"` |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// ObjectAttributePart is used by ObjectAttributesResponse to describe an object part |
| 107 | +type ObjectAttributePart struct { |
| 108 | + ChecksumCRC32 string `xml:",omitempty"` |
| 109 | + ChecksumCRC32C string `xml:",omitempty"` |
| 110 | + ChecksumSHA1 string `xml:",omitempty"` |
| 111 | + ChecksumSHA256 string `xml:",omitempty"` |
| 112 | + PartNumber int |
| 113 | + Size int |
| 114 | +} |
| 115 | + |
| 116 | +func (o *ObjectAttributes) parseResponse(resp *http.Response) (err error) { |
| 117 | + mod, err := parseRFC7231Time(resp.Header.Get("Last-Modified")) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + o.LastModified = mod |
| 122 | + o.VersionID = resp.Header.Get(amzVersionID) |
| 123 | + |
| 124 | + response := new(ObjectAttributesResponse) |
| 125 | + if err := xml.NewDecoder(resp.Body).Decode(response); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + o.ObjectAttributesResponse = *response |
| 129 | + |
| 130 | + return |
| 131 | +} |
| 132 | + |
| 133 | +// GetObjectAttributes API combines HeadObject and ListParts. |
| 134 | +// More details on usage can be found in the documentation for ObjectAttributesOptions{} |
| 135 | +func (c *Client) GetObjectAttributes(ctx context.Context, bucketName, objectName string, opts ObjectAttributesOptions) (*ObjectAttributes, error) { |
| 136 | + if err := s3utils.CheckValidBucketName(bucketName); err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + |
| 140 | + if err := s3utils.CheckValidObjectName(objectName); err != nil { |
| 141 | + return nil, err |
| 142 | + } |
| 143 | + |
| 144 | + urlValues := make(url.Values) |
| 145 | + urlValues.Add("attributes", "") |
| 146 | + if opts.VersionID != "" { |
| 147 | + urlValues.Add("versionId", opts.VersionID) |
| 148 | + } |
| 149 | + |
| 150 | + headers := make(http.Header) |
| 151 | + headers.Set(amzObjectAttributes, GetObjectAttributesTags) |
| 152 | + |
| 153 | + if opts.PartNumberMarker > 0 { |
| 154 | + headers.Set(amzPartNumberMarker, strconv.Itoa(opts.PartNumberMarker)) |
| 155 | + } |
| 156 | + |
| 157 | + if opts.MaxParts > 0 { |
| 158 | + headers.Set(amzMaxParts, strconv.Itoa(opts.MaxParts)) |
| 159 | + } else { |
| 160 | + headers.Set(amzMaxParts, strconv.Itoa(GetObjectAttributesMaxParts)) |
| 161 | + } |
| 162 | + |
| 163 | + if opts.ServerSideEncryption != nil { |
| 164 | + opts.ServerSideEncryption.Marshal(headers) |
| 165 | + } |
| 166 | + |
| 167 | + resp, err := c.executeMethod(ctx, http.MethodGet, requestMetadata{ |
| 168 | + bucketName: bucketName, |
| 169 | + objectName: objectName, |
| 170 | + queryValues: urlValues, |
| 171 | + contentSHA256Hex: emptySHA256Hex, |
| 172 | + customHeader: headers, |
| 173 | + }) |
| 174 | + if err != nil { |
| 175 | + return nil, err |
| 176 | + } |
| 177 | + |
| 178 | + defer closeResponse(resp) |
| 179 | + |
| 180 | + hasEtag := resp.Header.Get(ETag) |
| 181 | + if hasEtag != "" { |
| 182 | + return nil, errors.New("getObjectAttributes is not supported by the current endpoint version") |
| 183 | + } |
| 184 | + |
| 185 | + if resp.StatusCode != http.StatusOK { |
| 186 | + ER := new(ErrorResponse) |
| 187 | + if err := xml.NewDecoder(resp.Body).Decode(ER); err != nil { |
| 188 | + return nil, err |
| 189 | + } |
| 190 | + |
| 191 | + return nil, *ER |
| 192 | + } |
| 193 | + |
| 194 | + OA := new(ObjectAttributes) |
| 195 | + err = OA.parseResponse(resp) |
| 196 | + if err != nil { |
| 197 | + return nil, err |
| 198 | + } |
| 199 | + |
| 200 | + return OA, nil |
| 201 | +} |
0 commit comments