| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using Azure.Core; |
| | | 7 | | using Azure.Storage.Blobs.Models; |
| | | 8 | | |
| | | 9 | | namespace Azure.Storage.Blobs.Specialized |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// A <see cref="BlobBatch"/> allows you to batch multiple Azure Storage |
| | | 13 | | /// operations in a single request via <see cref="BlobBatchClient.SubmitBatch"/>. |
| | | 14 | | /// |
| | | 15 | | /// For more information, see |
| | | 16 | | /// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/blob-batch"> |
| | | 17 | | /// Blob Batch</see>. |
| | | 18 | | /// </summary> |
| | | 19 | | public class BlobBatch : IDisposable |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// The number of pending requests in the batch. |
| | | 23 | | /// </summary> |
| | 8 | 24 | | public int RequestCount => _messages.Count; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The <see cref="BlobBatchClient"/> associated with this batch. It |
| | | 28 | | /// provides the Uri, BatchOperationPipeline, etc. |
| | | 29 | | /// </summary> |
| | | 30 | | private readonly BlobBatchClient _client; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Storage requires each batch request to contain the same type of |
| | | 34 | | /// operation. |
| | | 35 | | /// </summary> |
| | | 36 | | private BlobBatchOperationType? _operationType = null; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The list of messages that will be sent as part of this batch. |
| | | 40 | | /// </summary> |
| | 0 | 41 | | private readonly IList<HttpMessage> _messages = new List<HttpMessage>(); |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// A value indicating whether the batch has already been submitted. |
| | | 45 | | /// </summary> |
| | 2528 | 46 | | internal bool Submitted { get; private set; } = false; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Creates a new instance of the <see cref="BlobBatch"/> for mocking. |
| | | 50 | | /// </summary> |
| | 0 | 51 | | protected BlobBatch() |
| | | 52 | | { |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Creates a new instance of the <see cref="BlobBatch"/> class. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="client"> |
| | | 59 | | /// The <see cref="BlobBatchClient"/> associated with this batch. |
| | | 60 | | /// </param> |
| | 116 | 61 | | public BlobBatch(BlobBatchClient client) => |
| | 116 | 62 | | _client = client ?? throw new ArgumentNullException(nameof(client)); |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets the list of messages to submit as part of this batch. |
| | | 66 | | /// |
| | | 67 | | /// Note that calling this method marks the batch as submitted and no |
| | | 68 | | /// further operations can be added. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <returns> |
| | | 71 | | /// The list of messages to submit as part of this batch. |
| | | 72 | | /// </returns> |
| | | 73 | | internal IList<HttpMessage> GetMessagesToSubmit() |
| | | 74 | | { |
| | 96 | 75 | | Submitted = true; |
| | 96 | 76 | | return _messages; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Verify whether the <paramref name="client"/> is the batch client |
| | | 81 | | /// associated with this batch. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="client">The BlobBatchClient to check.</param> |
| | | 84 | | internal bool IsAssociatedClient(BlobBatchClient client) => |
| | 100 | 85 | | _client == client; |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Set the batch operation type or throw if not allowed. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="operationType"> |
| | | 91 | | /// The type of operation to perform. |
| | | 92 | | /// </param> |
| | | 93 | | private void SetBatchOperationType(BlobBatchOperationType operationType) |
| | | 94 | | { |
| | 2332 | 95 | | if (Submitted) |
| | | 96 | | { |
| | 0 | 97 | | throw BatchErrors.BatchAlreadySubmitted(); |
| | | 98 | | } |
| | 2332 | 99 | | else if (_operationType != null && _operationType != operationType) |
| | | 100 | | { |
| | 8 | 101 | | throw BatchErrors.OnlyHomogenousOperationsAllowed(_operationType.Value); |
| | | 102 | | } |
| | 2324 | 103 | | _operationType = operationType; |
| | 2324 | 104 | | } |
| | | 105 | | |
| | | 106 | | #region DeleteBlob |
| | | 107 | | /// <summary> |
| | | 108 | | /// The <see cref="DeleteBlob(string, string, DeleteSnapshotsOption, BlobRequestConditions)"/> |
| | | 109 | | /// operation marks the specified blob or snapshot for deletion. The |
| | | 110 | | /// blob is later deleted during garbage collection. |
| | | 111 | | /// |
| | | 112 | | /// Note that in order to delete a blob, you must delete all of its |
| | | 113 | | /// snapshots. You can delete both at the same time using |
| | | 114 | | /// <see cref="DeleteSnapshotsOption.IncludeSnapshots"/>. |
| | | 115 | | /// |
| | | 116 | | /// For more information, see |
| | | 117 | | /// <see href="https://docs.microsoft.com/rest/api/storageservices/delete-blob">Delete Blob</see>. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="blobContainerName"> |
| | | 120 | | /// The name of the container containing the blob to delete. |
| | | 121 | | /// </param> |
| | | 122 | | /// <param name="blobName"> |
| | | 123 | | /// The name of the blob to delete. |
| | | 124 | | /// </param> |
| | | 125 | | /// <param name="snapshotsOption"> |
| | | 126 | | /// Specifies options for deleting blob snapshots. |
| | | 127 | | /// </param> |
| | | 128 | | /// <param name="conditions"> |
| | | 129 | | /// Optional <see cref="BlobRequestConditions"/> to add conditions on |
| | | 130 | | /// deleting this blob. |
| | | 131 | | /// </param> |
| | | 132 | | /// <returns> |
| | | 133 | | /// A <see cref="Response"/> on successfully deleting. The response |
| | | 134 | | /// cannot be used until the batch has been submitted with |
| | | 135 | | /// <see cref="BlobBatchClient.SubmitBatchAsync"/>. |
| | | 136 | | /// </returns> |
| | | 137 | | public virtual Response DeleteBlob( |
| | | 138 | | string blobContainerName, |
| | | 139 | | string blobName, |
| | | 140 | | DeleteSnapshotsOption snapshotsOption = default, |
| | | 141 | | BlobRequestConditions conditions = default) |
| | | 142 | | { |
| | 0 | 143 | | var blobUri = new BlobUriBuilder(_client.Uri) |
| | 0 | 144 | | { |
| | 0 | 145 | | BlobContainerName = blobContainerName, |
| | 0 | 146 | | BlobName = blobName |
| | 0 | 147 | | }; |
| | 0 | 148 | | return DeleteBlob( |
| | 0 | 149 | | blobUri.ToUri(), |
| | 0 | 150 | | snapshotsOption, |
| | 0 | 151 | | conditions); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// The <see cref="DeleteBlob(Uri, DeleteSnapshotsOption, BlobRequestConditions)"/> |
| | | 156 | | /// operation marks the specified blob or snapshot for deletion. The |
| | | 157 | | /// blob is later deleted during garbage collection. |
| | | 158 | | /// |
| | | 159 | | /// Note that in order to delete a blob, you must delete all of its |
| | | 160 | | /// snapshots. You can delete both at the same time using |
| | | 161 | | /// <see cref="DeleteSnapshotsOption.IncludeSnapshots"/>. |
| | | 162 | | /// |
| | | 163 | | /// For more information, see |
| | | 164 | | /// <see href="https://docs.microsoft.com/rest/api/storageservices/delete-blob">Delete Blob</see>. |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="blobUri"> |
| | | 167 | | /// The blob to delete's primary <see cref="Uri"/> endpoint. |
| | | 168 | | /// </param> |
| | | 169 | | /// <param name="snapshotsOption"> |
| | | 170 | | /// Specifies options for deleting blob snapshots. |
| | | 171 | | /// </param> |
| | | 172 | | /// <param name="conditions"> |
| | | 173 | | /// Optional <see cref="BlobRequestConditions"/> to add conditions on |
| | | 174 | | /// deleting this blob. |
| | | 175 | | /// </param> |
| | | 176 | | /// <returns> |
| | | 177 | | /// A <see cref="Response"/> on successfully deleting. The response |
| | | 178 | | /// cannot be used until the batch has been submitted with |
| | | 179 | | /// <see cref="BlobBatchClient.SubmitBatchAsync"/>. |
| | | 180 | | /// </returns> |
| | | 181 | | public virtual Response DeleteBlob( |
| | | 182 | | Uri blobUri, |
| | | 183 | | DeleteSnapshotsOption snapshotsOption = default, |
| | | 184 | | BlobRequestConditions conditions = default) |
| | | 185 | | { |
| | 2228 | 186 | | SetBatchOperationType(BlobBatchOperationType.Delete); |
| | 2224 | 187 | | HttpMessage message = BatchRestClient.Blob.DeleteAsync_CreateMessage( |
| | 2224 | 188 | | _client.BatchOperationPipeline, |
| | 2224 | 189 | | blobUri, |
| | 2224 | 190 | | version: _client.Version.ToVersionString(), |
| | 2224 | 191 | | deleteSnapshots: snapshotsOption == DeleteSnapshotsOption.None ? null : (DeleteSnapshotsOption?)snapshot |
| | 2224 | 192 | | leaseId: conditions?.LeaseId, |
| | 2224 | 193 | | ifModifiedSince: conditions?.IfModifiedSince, |
| | 2224 | 194 | | ifUnmodifiedSince: conditions?.IfUnmodifiedSince, |
| | 2224 | 195 | | ifMatch: conditions?.IfMatch, |
| | 2224 | 196 | | ifNoneMatch: conditions?.IfNoneMatch); |
| | 2224 | 197 | | _messages.Add(message); |
| | 3328 | 198 | | return new DelayedResponse(message, response => BatchRestClient.Blob.DeleteAsync_CreateResponse(_client.Clie |
| | | 199 | | } |
| | | 200 | | #endregion DeleteBlob |
| | | 201 | | |
| | | 202 | | #region SetBlobAccessTier |
| | | 203 | | /// <summary> |
| | | 204 | | /// The <see cref="SetBlobAccessTier(string, string, AccessTier, RehydratePriority?, BlobRequestConditions)"/> |
| | | 205 | | /// operation sets the tier on a blob. The operation is allowed on |
| | | 206 | | /// block blobs in a blob storage or general purpose v2 account. |
| | | 207 | | /// |
| | | 208 | | /// A block blob's tier determines Hot/Cool/Archive storage type. This |
| | | 209 | | /// operation does not update the blob's ETag. For detailed |
| | | 210 | | /// information about block blob level tiering see |
| | | 211 | | /// <see href="https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-storage-tiers"> |
| | | 212 | | /// Blob Storage Tiers</see>. |
| | | 213 | | /// </summary> |
| | | 214 | | /// <param name="blobContainerName"> |
| | | 215 | | /// The name of the container containing the blob to set the tier of. |
| | | 216 | | /// </param> |
| | | 217 | | /// <param name="blobName"> |
| | | 218 | | /// The name of the blob to set the tier of. |
| | | 219 | | /// </param> |
| | | 220 | | /// <param name="accessTier"> |
| | | 221 | | /// Indicates the tier to be set on the blob. |
| | | 222 | | /// </param> |
| | | 223 | | /// <param name="leaseAccessConditions"> |
| | | 224 | | /// Optional <see cref="BlobRequestConditions"/> to add conditions on |
| | | 225 | | /// setting the access tier. |
| | | 226 | | /// </param> |
| | | 227 | | /// <param name="rehydratePriority"> |
| | | 228 | | /// Optional <see cref="RehydratePriority"/> |
| | | 229 | | /// Indicates the priority with which to rehydrate an archived blob. |
| | | 230 | | /// </param> |
| | | 231 | | /// <returns> |
| | | 232 | | /// A <see cref="Response"/> on successfully deleting. The response |
| | | 233 | | /// cannot be used until the batch has been submitted with |
| | | 234 | | /// <see cref="BlobBatchClient.SubmitBatchAsync"/>. |
| | | 235 | | /// </returns> |
| | | 236 | | public virtual Response SetBlobAccessTier( |
| | | 237 | | string blobContainerName, |
| | | 238 | | string blobName, |
| | | 239 | | AccessTier accessTier, |
| | | 240 | | RehydratePriority? rehydratePriority = default, |
| | | 241 | | BlobRequestConditions leaseAccessConditions = default) |
| | | 242 | | { |
| | 0 | 243 | | var blobUri = new BlobUriBuilder(_client.Uri) |
| | 0 | 244 | | { |
| | 0 | 245 | | BlobContainerName = blobContainerName, |
| | 0 | 246 | | BlobName = blobName |
| | 0 | 247 | | }; |
| | 0 | 248 | | return SetBlobAccessTier( |
| | 0 | 249 | | blobUri.ToUri(), |
| | 0 | 250 | | accessTier, |
| | 0 | 251 | | rehydratePriority, |
| | 0 | 252 | | leaseAccessConditions); |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// The <see cref="SetBlobAccessTier(Uri, AccessTier, RehydratePriority?, BlobRequestConditions)"/> |
| | | 257 | | /// operation sets the tier on a blob. The operation is allowed on |
| | | 258 | | /// block blobs in a blob storage or general purpose v2 account. |
| | | 259 | | /// |
| | | 260 | | /// A block blob's tier determines Hot/Cool/Archive storage type. This |
| | | 261 | | /// operation does not update the blob's ETag. For detailed |
| | | 262 | | /// information about block blob level tiering |
| | | 263 | | /// <see href="https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-storage-tiers"> |
| | | 264 | | /// Blob Storage Tiers</see>. |
| | | 265 | | /// |
| | | 266 | | /// </summary> |
| | | 267 | | /// <param name="blobUri"> |
| | | 268 | | /// The blob's primary <see cref="Uri"/> endpoint. |
| | | 269 | | /// </param> |
| | | 270 | | /// <param name="accessTier"> |
| | | 271 | | /// Indicates the tier to be set on the blob. |
| | | 272 | | /// </param> |
| | | 273 | | /// <param name="rehydratePriority"> |
| | | 274 | | /// Optional <see cref="RehydratePriority"/> |
| | | 275 | | /// Indicates the priority with which to rehydrate an archived blob. |
| | | 276 | | /// </param> |
| | | 277 | | /// <param name="leaseAccessConditions"> |
| | | 278 | | /// Optional <see cref="BlobRequestConditions"/> to add conditions on |
| | | 279 | | /// setting the access tier. |
| | | 280 | | /// </param> |
| | | 281 | | /// <returns> |
| | | 282 | | /// A <see cref="Response"/> on successfully deleting. The response |
| | | 283 | | /// cannot be used until the batch has been submitted with |
| | | 284 | | /// <see cref="BlobBatchClient.SubmitBatchAsync"/>. |
| | | 285 | | /// </returns> |
| | | 286 | | public virtual Response SetBlobAccessTier( |
| | | 287 | | Uri blobUri, |
| | | 288 | | AccessTier accessTier, |
| | | 289 | | RehydratePriority? rehydratePriority = default, |
| | | 290 | | BlobRequestConditions leaseAccessConditions = default) |
| | | 291 | | { |
| | 104 | 292 | | SetBatchOperationType(BlobBatchOperationType.SetAccessTier); |
| | 100 | 293 | | HttpMessage message = BatchRestClient.Blob.SetAccessTierAsync_CreateMessage( |
| | 100 | 294 | | _client.BatchOperationPipeline, |
| | 100 | 295 | | blobUri, |
| | 100 | 296 | | tier: accessTier, |
| | 100 | 297 | | version: _client.Version.ToVersionString(), |
| | 100 | 298 | | rehydratePriority: rehydratePriority, |
| | 100 | 299 | | leaseId: leaseAccessConditions?.LeaseId); |
| | 100 | 300 | | _messages.Add(message); |
| | 160 | 301 | | return new DelayedResponse(message, response => BatchRestClient.Blob.SetAccessTierAsync_CreateResponse(_clie |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Dispose all messages in the batch. |
| | | 306 | | /// </summary> |
| | | 307 | | public void Dispose() |
| | | 308 | | { |
| | 88 | 309 | | GC.SuppressFinalize(this); |
| | 560 | 310 | | foreach (HttpMessage message in _messages) { |
| | 192 | 311 | | message.Dispose(); |
| | | 312 | | } |
| | 88 | 313 | | } |
| | | 314 | | #endregion SetBlobAccessTier |
| | | 315 | | } |
| | | 316 | | } |