| 1 | package blobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/Azure/go-autorest/autorest" |
| 11 | "github.com/Azure/go-autorest/autorest/azure" |
| 12 | "github.com/Azure/go-autorest/autorest/validation" |
| 13 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 14 | "github.com/tombuildsstuff/giovanni/storage/internal/metadata" |
| 15 | ) |
| 16 | |
| 17 | type GetPropertiesInput struct { |
| 18 | // The ID of the Lease |
| 19 | // This must be specified if a Lease is present on the Blob, else a 403 is returned |
| 20 | LeaseID *string |
| 21 | } |
| 22 | |
| 23 | type GetPropertiesResult struct { |
| 24 | autorest.Response |
| 25 | |
| 26 | // The tier of page blob on a premium storage account or tier of block blob on blob storage or general purpose v2 account. |
| 27 | AccessTier AccessTier |
| 28 | |
| 29 | // This gives the last time tier was changed on the object. |
| 30 | // This header is returned only if tier on block blob was ever set. |
| 31 | // The date format follows RFC 1123 |
| 32 | AccessTierChangeTime string |
| 33 | |
| 34 | // For page blobs on a premium storage account only. |
| 35 | // If the access tier is not explicitly set on the blob, the tier is inferred based on its content length |
| 36 | // and this header will be returned with true value. |
| 37 | // For block blobs on Blob Storage or general purpose v2 account, if the blob does not have the access tier |
| 38 | // set then we infer the tier from the storage account properties. This header is set only if the block blob |
| 39 | // tier is inferred |
| 40 | AccessTierInferred bool |
| 41 | |
| 42 | // For blob storage or general purpose v2 account. |
| 43 | // If the blob is being rehydrated and is not complete then this header is returned indicating |
| 44 | // that rehydrate is pending and also tells the destination tier |
| 45 | ArchiveStatus ArchiveStatus |
| 46 | |
| 47 | // The number of committed blocks present in the blob. |
| 48 | // This header is returned only for append blobs. |
| 49 | BlobCommittedBlockCount string |
| 50 | |
| 51 | // The current sequence number for a page blob. |
| 52 | // This header is not returned for block blobs or append blobs. |
| 53 | // This header is not returned for block blobs. |
| 54 | BlobSequenceNumber string |
| 55 | |
| 56 | // The blob type. |
| 57 | BlobType BlobType |
| 58 | |
| 59 | // If the Cache-Control request header has previously been set for the blob, that value is returned in this header. |
| 60 | CacheControl string |
| 61 | |
| 62 | // The Content-Disposition response header field conveys additional information about how to process |
| 63 | // the response payload, and also can be used to attach additional metadata. |
| 64 | // For example, if set to attachment, it indicates that the user-agent should not display the response, |
| 65 | // but instead show a Save As dialog. |
| 66 | ContentDisposition string |
| 67 | |
| 68 | // If the Content-Encoding request header has previously been set for the blob, |
| 69 | // that value is returned in this header. |
| 70 | ContentEncoding string |
| 71 | |
| 72 | // If the Content-Language request header has previously been set for the blob, |
| 73 | // that value is returned in this header. |
| 74 | ContentLanguage string |
| 75 | |
| 76 | // The size of the blob in bytes. |
| 77 | // For a page blob, this header returns the value of the x-ms-blob-content-length header stored with the blob. |
| 78 | ContentLength int64 |
| 79 | |
| 80 | // The content type specified for the blob. |
| 81 | // If no content type was specified, the default content type is `application/octet-stream`. |
| 82 | ContentType string |
| 83 | |
| 84 | // If the Content-MD5 header has been set for the blob, this response header is returned so that |
| 85 | // the client can check for message content integrity. |
| 86 | ContentMD5 string |
| 87 | |
| 88 | // Conclusion time of the last attempted Copy Blob operation where this blob was the destination blob. |
| 89 | // This value can specify the time of a completed, aborted, or failed copy attempt. |
| 90 | // This header does not appear if a copy is pending, if this blob has never been the |
| 91 | // destination in a Copy Blob operation, or if this blob has been modified after a concluded Copy Blob |
| 92 | // operation using Set Blob Properties, Put Blob, or Put Block List. |
| 93 | CopyCompletionTime string |
| 94 | |
| 95 | // Included if the blob is incremental copy blob or incremental copy snapshot, if x-ms-copy-status is success. |
| 96 | // Snapshot time of the last successful incremental copy snapshot for this blob |
| 97 | CopyDestinationSnapshot string |
| 98 | |
| 99 | // String identifier for the last attempted Copy Blob operation where this blob was the destination blob. |
| 100 | // This header does not appear if this blob has never been the destination in a Copy Blob operation, |
| 101 | // or if this blob has been modified after a concluded Copy Blob operation using Set Blob Properties, |
| 102 | // Put Blob, or Put Block List. |
| 103 | CopyID string |
| 104 | |
| 105 | // Contains the number of bytes copied and the total bytes in the source in the last attempted |
| 106 | // Copy Blob operation where this blob was the destination blob. |
| 107 | // Can show between 0 and Content-Length bytes copied. |
| 108 | // This header does not appear if this blob has never been the destination in a Copy Blob operation, |
| 109 | // or if this blob has been modified after a concluded Copy Blob operation using Set Blob Properties, |
| 110 | // Put Blob, or Put Block List. |
| 111 | CopyProgress string |
| 112 | |
| 113 | // URL up to 2 KB in length that specifies the source blob used in the last attempted Copy Blob operation |
| 114 | // where this blob was the destination blob. |
| 115 | // This header does not appear if this blob has never been the destination in a Copy Blob operation, |
| 116 | // or if this blob has been modified after a concluded Copy Blob operation using Set Blob Properties, |
| 117 | // Put Blob, or Put Block List |
| 118 | CopySource string |
| 119 | |
| 120 | // State of the copy operation identified by x-ms-copy-id, with these values: |
| 121 | // - success: Copy completed successfully. |
| 122 | // - pending: Copy is in progress. |
| 123 | // Check x-ms-copy-status-description if intermittent, non-fatal errors |
| 124 | // impede copy progress but don’t cause failure. |
| 125 | // - aborted: Copy was ended by Abort Copy Blob. |
| 126 | // - failed: Copy failed. See x-ms- copy-status-description for failure details. |
| 127 | // This header does not appear if this blob has never been the destination in a Copy Blob operation, |
| 128 | // or if this blob has been modified after a completed Copy Blob operation using Set Blob Properties, |
| 129 | // Put Blob, or Put Block List. |
| 130 | CopyStatus CopyStatus |
| 131 | |
| 132 | // Describes cause of fatal or non-fatal copy operation failure. |
| 133 | // This header does not appear if this blob has never been the destination in a Copy Blob operation, |
| 134 | // or if this blob has been modified after a concluded Copy Blob operation using Set Blob Properties, |
| 135 | // Put Blob, or Put Block List. |
| 136 | CopyStatusDescription string |
| 137 | |
| 138 | // The date/time at which the blob was created. The date format follows RFC 1123 |
| 139 | CreationTime string |
| 140 | |
| 141 | // The ETag contains a value that you can use to perform operations conditionally |
| 142 | ETag string |
| 143 | |
| 144 | // Included if the blob is incremental copy blob. |
| 145 | IncrementalCopy bool |
| 146 | |
| 147 | // The date/time that the blob was last modified. The date format follows RFC 1123. |
| 148 | LastModified string |
| 149 | |
| 150 | // When a blob is leased, specifies whether the lease is of infinite or fixed duration |
| 151 | LeaseDuration LeaseDuration |
| 152 | |
| 153 | // The lease state of the blob |
| 154 | LeaseState LeaseState |
| 155 | |
| 156 | LeaseStatus LeaseStatus |
| 157 | |
| 158 | // A set of name-value pairs that correspond to the user-defined metadata associated with this blob |
| 159 | MetaData map[string]string |
| 160 | |
| 161 | // Is the Storage Account encrypted using server-side encryption? This should always return true |
| 162 | ServerEncrypted bool |
| 163 | } |
| 164 | |
| 165 | // GetProperties returns all user-defined metadata, standard HTTP properties, and system properties for the blob |
| 166 | func (client Client) GetProperties(ctx context.Context, accountName, containerName, blobName string, input GetPropertiesInput) (result GetPropertiesResult, err error) { |
| 167 | if accountName == "" { |
| 168 | return result, validation.NewError("blobs.Client", "GetProperties", "`accountName` cannot be an empty string.") |
| 169 | } |
| 170 | if containerName == "" { |
| 171 | return result, validation.NewError("blobs.Client", "GetProperties", "`containerName` cannot be an empty string.") |
| 172 | } |
| 173 | if strings.ToLower(containerName) != containerName { |
| 174 | return result, validation.NewError("blobs.Client", "GetProperties", "`containerName` must be a lower-cased string.") |
| 175 | } |
| 176 | if blobName == "" { |
| 177 | return result, validation.NewError("blobs.Client", "GetProperties", "`blobName` cannot be an empty string.") |
| 178 | } |
| 179 | |
| 180 | req, err := client.GetPropertiesPreparer(ctx, accountName, containerName, blobName, input) |
| 181 | if err != nil { |
| 182 | err = autorest.NewErrorWithError(err, "blobs.Client", "GetProperties", nil, "Failure preparing request") |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | resp, err := client.GetPropertiesSender(req) |
| 187 | if err != nil { |
| 188 | result.Response = autorest.Response{Response: resp} |
| 189 | err = autorest.NewErrorWithError(err, "blobs.Client", "GetProperties", resp, "Failure sending request") |
| 190 | return |
| 191 | } |
| 192 | |
| 193 | result, err = client.GetPropertiesResponder(resp) |
| 194 | if err != nil { |
| 195 | err = autorest.NewErrorWithError(err, "blobs.Client", "GetProperties", resp, "Failure responding to request") |
| 196 | return |
| 197 | } |
| 198 | |
| 199 | return |
| 200 | } |
| 201 | |
| 202 | // GetPropertiesPreparer prepares the GetProperties request. |
| 203 | func (client Client) GetPropertiesPreparer(ctx context.Context, accountName, containerName, blobName string, input GetPropertiesInput) (*http.Request, error) { |
| 204 | pathParameters := map[string]interface{}{ |
| 205 | "containerName": autorest.Encode("path", containerName), |
| 206 | "blobName": autorest.Encode("path", blobName), |
| 207 | } |
| 208 | |
| 209 | headers := map[string]interface{}{ |
| 210 | "x-ms-version": APIVersion, |
| 211 | } |
| 212 | |
| 213 | if input.LeaseID != nil { |
| 214 | headers["x-ms-lease-id"] = *input.LeaseID |
| 215 | } |
| 216 | |
| 217 | preparer := autorest.CreatePreparer( |
| 218 | autorest.AsHead(), |
| 219 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 220 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 221 | autorest.WithHeaders(headers)) |
| 222 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 223 | } |
| 224 | |
| 225 | // GetPropertiesSender sends the GetProperties request. The method will close the |
| 226 | // http.Response Body if it receives an error. |
| 227 | func (client Client) GetPropertiesSender(req *http.Request) (*http.Response, error) { |
| 228 | return autorest.SendWithSender(client, req, |
| 229 | azure.DoRetryWithRegistration(client.Client)) |
| 230 | } |
| 231 | |
| 232 | // GetPropertiesResponder handles the response to the GetProperties request. The method always |
| 233 | // closes the http.Response Body. |
| 234 | func (client Client) GetPropertiesResponder(resp *http.Response) (result GetPropertiesResult, err error) { |
| 235 | if resp != nil && resp.Header != nil { |
| 236 | result.AccessTier = AccessTier(resp.Header.Get("x-ms-access-tier")) |
| 237 | result.AccessTierChangeTime = resp.Header.Get(" x-ms-access-tier-change-time") |
| 238 | result.ArchiveStatus = ArchiveStatus(resp.Header.Get(" x-ms-archive-status")) |
| 239 | result.BlobCommittedBlockCount = resp.Header.Get("x-ms-blob-committed-block-count") |
| 240 | result.BlobSequenceNumber = resp.Header.Get("x-ms-blob-sequence-number") |
| 241 | result.BlobType = BlobType(resp.Header.Get("x-ms-blob-type")) |
| 242 | result.CacheControl = resp.Header.Get("Cache-Control") |
| 243 | result.ContentDisposition = resp.Header.Get("Content-Disposition") |
| 244 | result.ContentEncoding = resp.Header.Get("Content-Encoding") |
| 245 | result.ContentLanguage = resp.Header.Get("Content-Language") |
| 246 | result.ContentMD5 = resp.Header.Get("Content-MD5") |
| 247 | result.ContentType = resp.Header.Get("Content-Type") |
| 248 | result.CopyCompletionTime = resp.Header.Get("x-ms-copy-completion-time") |
| 249 | result.CopyDestinationSnapshot = resp.Header.Get("x-ms-copy-destination-snapshot") |
| 250 | result.CopyID = resp.Header.Get("x-ms-copy-id") |
| 251 | result.CopyProgress = resp.Header.Get("x-ms-copy-progress") |
| 252 | result.CopySource = resp.Header.Get("x-ms-copy-source") |
| 253 | result.CopyStatus = CopyStatus(resp.Header.Get("x-ms-copy-status")) |
| 254 | result.CopyStatusDescription = resp.Header.Get("x-ms-copy-status-description") |
| 255 | result.CreationTime = resp.Header.Get("x-ms-creation-time") |
| 256 | result.ETag = resp.Header.Get("Etag") |
| 257 | result.LastModified = resp.Header.Get("Last-Modified") |
| 258 | result.LeaseDuration = LeaseDuration(resp.Header.Get("x-ms-lease-duration")) |
| 259 | result.LeaseState = LeaseState(resp.Header.Get("x-ms-lease-state")) |
| 260 | result.LeaseStatus = LeaseStatus(resp.Header.Get("x-ms-lease-status")) |
| 261 | result.MetaData = metadata.ParseFromHeaders(resp.Header) |
| 262 | |
| 263 | if v := resp.Header.Get("x-ms-access-tier-inferred"); v != "" { |
| 264 | b, innerErr := strconv.ParseBool(v) |
| 265 | if innerErr != nil { |
| 266 | err = fmt.Errorf("Error parsing %q as a bool: %s", v, innerErr) |
| 267 | return |
| 268 | } |
| 269 | |
| 270 | result.AccessTierInferred = b |
| 271 | } |
| 272 | |
| 273 | if v := resp.Header.Get("Content-Length"); v != "" { |
| 274 | i, innerErr := strconv.Atoi(v) |
| 275 | if innerErr != nil { |
| 276 | err = fmt.Errorf("Error parsing %q as an integer: %s", v, innerErr) |
| 277 | } |
| 278 | |
| 279 | result.ContentLength = int64(i) |
| 280 | } |
| 281 | |
| 282 | if v := resp.Header.Get("x-ms-incremental-copy"); v != "" { |
| 283 | b, innerErr := strconv.ParseBool(v) |
| 284 | if innerErr != nil { |
| 285 | err = fmt.Errorf("Error parsing %q as a bool: %s", v, innerErr) |
| 286 | return |
| 287 | } |
| 288 | |
| 289 | result.IncrementalCopy = b |
| 290 | } |
| 291 | |
| 292 | if v := resp.Header.Get("x-ms-server-encrypted"); v != "" { |
| 293 | b, innerErr := strconv.ParseBool(v) |
| 294 | if innerErr != nil { |
| 295 | err = fmt.Errorf("Error parsing %q as a bool: %s", v, innerErr) |
| 296 | return |
| 297 | } |
| 298 | |
| 299 | result.IncrementalCopy = b |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | err = autorest.Respond( |
| 304 | resp, |
| 305 | client.ByInspecting(), |
| 306 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 307 | autorest.ByClosing()) |
| 308 | result.Response = autorest.Response{Response: resp} |
| 309 | return |
| 310 | } |