/// <summary> /// Deletes the blob if it already exists asynchronously. /// </summary> /// <param name="cloudBlob">Cloud blob client.</param> /// <param name="deleteSnapshotsOption">Whether to only delete the blob, to delete the blob and all snapshots, or to only delete the snapshots.</param> /// <param name="accessCondition"> /// An <see cref="T:Microsoft.WindowsAzure.Storage.AccessCondition" /> object that represents the access conditions for the blob. /// </param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns> /// <c>true</c> if the blob did not already exist and was created; otherwise <c>false</c>. /// </returns> public static Task<bool> DeleteIfExistsAsync( this ICloudBlob cloudBlob, DeleteSnapshotsOption deleteSnapshotsOption = DeleteSnapshotsOption.None, AccessCondition accessCondition = null, CancellationToken cancellationToken = default (CancellationToken)) { ICancellableAsyncResult asyncResult = cloudBlob.BeginDeleteIfExists(deleteSnapshotsOption, accessCondition, null, null, null, null); CancellationTokenRegistration registration = cancellationToken.Register(p => asyncResult.Cancel(), null); return Task<bool>.Factory.FromAsync( asyncResult, result => { registration.Dispose(); return cloudBlob.EndDeleteIfExists(result); }); }
/// <summary> /// Deletes the container if it already exists asynchronously. /// </summary> /// <param name="blobContainer">Cloud blob container.</param> /// <param name="accessCondition"> /// An <see cref="T:Microsoft.WindowsAzure.Storage.AccessCondition" /> object that represents the access conditions for the container. If <c>null</c>, no condition is used. /// </param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns> /// <c>true</c> if the container did not already exist and was created; otherwise <c>false</c>. /// </returns> public static Task<bool> DeleteIfExistsAsync( this CloudBlobContainer blobContainer, AccessCondition accessCondition = null, CancellationToken cancellationToken = default (CancellationToken)) { ICancellableAsyncResult asyncResult = blobContainer.BeginDeleteIfExists(accessCondition, null, null, null, null); CancellationTokenRegistration registration = cancellationToken.Register(p => asyncResult.Cancel(), null); return Task<bool>.Factory.FromAsync( asyncResult, result => { registration.Dispose(); return blobContainer.EndDeleteIfExists(result); }); }