/// <summary> /// Return a task that asynchronously delete the specified container. /// </summary> /// <param name="container">CloudBlobContainer object</param> /// <param name="accessCondition">Access condition</param> /// <param name="requestOptions">Blob request option</param> /// <param name="operationContext">Operation context</param> /// <param name="cmdletCancellationToken">Cancellation token</param> /// <returns>Return a task that asynchronously delete the specified container.</returns> public Task DeleteContainerAsync(CloudBlobContainer container, AccessCondition accessCondition, BlobRequestOptions requestOptions, OperationContext operationContext, CancellationToken cancellationToken) { return container.DeleteAsync(accessCondition, requestOptions, operationContext, cancellationToken); }
/// <summary> /// Test container deletion, expecting lease failure. /// </summary> /// <param name="testContainer">The container.</param> /// <param name="testAccessCondition">The failing access condition to use.</param> /// <param name="expectedErrorCode">The expected error code.</param> /// <param name="description">The reason why these calls should fail.</param> private async Task ContainerDeleteExpectLeaseFailureAsync(CloudBlobContainer testContainer, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description) { OperationContext operationContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async () => await testContainer.DeleteAsync(testAccessCondition, null /* options */, operationContext), operationContext, description + " (Delete)", expectedStatusCode, expectedErrorCode); }
/// <summary> /// Test container deletion, expecting success. /// </summary> /// <param name="testContainer">The container.</param> /// <param name="testAccessCondition">The access condition to use.</param> private async Task ContainerDeleteExpectLeaseSuccessAsync(CloudBlobContainer testContainer, AccessCondition testAccessCondition) { await testContainer.DeleteAsync(testAccessCondition, null /* options */, null); }
/// <summary> /// Test container deletion, expecting success. /// </summary> /// <param name="testContainer">The container.</param> /// <param name="testAccessCondition">The access condition to use.</param> private void ContainerDeleteExpectLeaseSuccessTask(CloudBlobContainer testContainer, AccessCondition testAccessCondition) { testContainer.DeleteAsync(testAccessCondition, null /* options */, null /* operationContext */).Wait(); }
/// <summary> /// Verifies the behavior of a lease while the lease holds. Once the lease expires, this method confirms that write operations succeed. /// The test is cut short once the <c>testLength</c> time has elapsed. /// </summary> /// <param name="leasedContainer">The container.</param> /// <param name="duration">The duration of the lease.</param> /// <param name="testLength">The maximum length of time to run the test.</param> /// <param name="tolerance">The allowed lease time error.</param> internal async Task ContainerAcquireRenewLeaseTestAsync(CloudBlobContainer leasedContainer, TimeSpan? duration, TimeSpan testLength, TimeSpan tolerance) { OperationContext operationContext = new OperationContext(); DateTime beginTime = DateTime.UtcNow; while (true) { try { // Attempt to delete the container with no lease ID. await leasedContainer.DeleteAsync(null, null, operationContext); // The delete succeeded, which means that the lease must have expired. // If the lease was infinite then there is an error because it should not have expired. Assert.IsNotNull(duration, "An infinite lease should not expire."); // The lease should be past its expiration time. Assert.IsTrue(DateTime.UtcNow - beginTime > duration - tolerance, "Deletes should not succeed while lease is present."); // Since the lease has expired (and the container was deleted), the test is over. return; } catch (Exception) { if (operationContext.LastResult.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.LeaseIdMissing) { // We got this error because the lease has not expired yet. // Make sure the lease is not past its expiration time yet. DateTime currentTime = DateTime.UtcNow; if (duration.HasValue) { Assert.IsTrue(currentTime - beginTime < duration + tolerance, "Deletes should succeed after a lease expires."); } // End the test early if necessary if (currentTime - beginTime > testLength) { // The lease has not expired, but we're not waiting any longer. return; } } else { throw; } } // Attempt to write to and read from the container. This should always succeed. await leasedContainer.SetMetadataAsync(); await leasedContainer.FetchAttributesAsync(); // Wait 1 second before trying again. await Task.Delay(TimeSpan.FromSeconds(1)); } }
/// <summary> /// Test container deletion, expecting lease failure. /// </summary> /// <param name="testContainer">The container.</param> /// <param name="testAccessCondition">The failing access condition to use.</param> /// <param name="expectedErrorCode">The expected error code.</param> /// <param name="description">The reason why these calls should fail.</param> private void ContainerDeleteExpectLeaseFailureTask(CloudBlobContainer testContainer, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description) { TestHelper.ExpectedExceptionTask( testContainer.DeleteAsync(testAccessCondition, null /* options */, null /* operationContext */), description + " (Delete)", expectedStatusCode, expectedErrorCode); }
public async Task PageBlobWriteStreamBasicTestAsync() { byte[] buffer = GetRandomBuffer(6 * 512); CloudBlobContainer container = GetRandomContainerReference(); container.ServiceClient.ParallelOperationThreadCount = 2; try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamWriteSizeInBytes = 8 * 512; using (MemoryStream wholeBlob = new MemoryStream()) { using (Stream writeStream = await blob.OpenWriteAsync(buffer.Length * 3)) { Stream blobStream = writeStream; for (int i = 0; i < 3; i++) { await blobStream.WriteAsync(buffer, 0, buffer.Length); await wholeBlob.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); } await blobStream.FlushAsync(); } using (MemoryStream downloadedBlob = new MemoryStream()) { await blob.DownloadToStreamAsync(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob); } using (Stream writeStream = await blob.OpenWriteAsync(null)) { Stream blobStream = writeStream; blobStream.Seek(buffer.Length / 2, SeekOrigin.Begin); wholeBlob.Seek(buffer.Length / 2, SeekOrigin.Begin); for (int i = 0; i < 2; i++) { blobStream.Write(buffer, 0, buffer.Length); wholeBlob.Write(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); } await blobStream.FlushAsync(); } using (MemoryStream downloadedBlob = new MemoryStream()) { await blob.DownloadToStreamAsync(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob); } } } finally { container.DeleteAsync().Wait(); } }
public async Task PageBlobWriteStreamOpenWithAccessConditionAsync() { CloudBlobContainer container = GetRandomContainerReference(); await container.CreateAsync(); try { OperationContext context = new OperationContext(); CloudPageBlob existingBlob = container.GetPageBlobReference("blob"); await existingBlob.CreateAsync(1024); CloudPageBlob blob = container.GetPageBlobReference("blob2"); AccessCondition accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await blob.OpenWriteAsync(1024, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); blob = container.GetPageBlobReference("blob3"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(existingBlob.Properties.ETag); Stream blobStream = await blob.OpenWriteAsync(1024, accessCondition, null, context); blobStream.Dispose(); blob = container.GetPageBlobReference("blob4"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); blobStream = await blob.OpenWriteAsync(1024, accessCondition, null, context); blobStream.Dispose(); blob = container.GetPageBlobReference("blob5"); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); blobStream = await blob.OpenWriteAsync(1024, accessCondition, null, context); blobStream.Dispose(); blob = container.GetPageBlobReference("blob6"); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); blobStream = await blob.OpenWriteAsync(1024, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(1024, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfMatchCondition(blob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(1024, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(blob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(1024, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(existingBlob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(1024, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(1024, accessCondition, null, context), context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.Conflict); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); blobStream = await existingBlob.OpenWriteAsync(1024, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(1024, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); blobStream = await existingBlob.OpenWriteAsync(1024, accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(1024, accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); } finally { container.DeleteAsync().Wait(); } }
public async Task BlockBlobWriteStreamOpenWithAccessConditionAsync() { CloudBlobContainer container = GetRandomContainerReference(); await container.CreateAsync(); try { OperationContext context = new OperationContext(); CloudBlockBlob existingBlob = container.GetBlockBlobReference("blob"); await existingBlob.PutBlockListAsync(new List <string>()); CloudBlockBlob blob = container.GetBlockBlobReference("blob2"); AccessCondition accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await blob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.NotFound); blob = container.GetBlockBlobReference("blob3"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(existingBlob.Properties.ETag); IOutputStream blobStream = await blob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); blob = container.GetBlockBlobReference("blob4"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); blobStream = await blob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); blob = container.GetBlockBlobReference("blob5"); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); blobStream = await blob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); blob = container.GetBlockBlobReference("blob6"); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); blobStream = await blob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfMatchCondition(blob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(blob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfNoneMatchCondition(existingBlob.Properties.ETag); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.NotModified); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); await TestHelper.ExpectedExceptionAsync( () => { blobStream.Dispose(); return(Task.FromResult(true)); }, context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.Conflict); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.NotModified); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(1)); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); blobStream.Dispose(); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value.AddMinutes(-1)); await TestHelper.ExpectedExceptionAsync( async() => await existingBlob.OpenWriteAsync(accessCondition, null, context), context, "OpenWriteAsync with a non-met condition should fail", HttpStatusCode.PreconditionFailed); accessCondition = AccessCondition.GenerateIfMatchCondition(existingBlob.Properties.ETag); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); await existingBlob.SetPropertiesAsync(); await TestHelper.ExpectedExceptionAsync( () => { blobStream.Dispose(); return(Task.FromResult(true)); }, context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.PreconditionFailed); blob = container.GetBlockBlobReference("blob7"); accessCondition = AccessCondition.GenerateIfNoneMatchCondition("*"); blobStream = await blob.OpenWriteAsync(accessCondition, null, context); await blob.PutBlockListAsync(new List <string>()); await TestHelper.ExpectedExceptionAsync( () => { blobStream.Dispose(); return(Task.FromResult(true)); }, context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.Conflict); blob = container.GetBlockBlobReference("blob8"); accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(existingBlob.Properties.LastModified.Value); blobStream = await existingBlob.OpenWriteAsync(accessCondition, null, context); await existingBlob.SetPropertiesAsync(); await TestHelper.ExpectedExceptionAsync( () => { blobStream.Dispose(); return(Task.FromResult(true)); }, context, "BlobWriteStream.Dispose with a non-met condition should fail", HttpStatusCode.PreconditionFailed); } finally { container.DeleteAsync().AsTask().Wait(); } }
public async Task PageBlobWriteStreamBasicTestAsync() { byte[] buffer = GetRandomBuffer(6 * 512); CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash(); CloudBlobContainer container = GetRandomContainerReference(); container.ServiceClient.ParallelOperationThreadCount = 2; try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamWriteSizeInBytes = 8 * 512; using (MemoryStream wholeBlob = new MemoryStream()) { BlobRequestOptions options = new BlobRequestOptions() { StoreBlobContentMD5 = true, }; using (IOutputStream writeStream = await blob.OpenWriteAsync(buffer.Length * 3, null, options, null)) { Stream blobStream = writeStream.AsStreamForWrite(); for (int i = 0; i < 3; i++) { await blobStream.WriteAsync(buffer, 0, buffer.Length); await wholeBlob.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); hasher.Append(buffer.AsBuffer()); } } string md5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset()); await blob.FetchAttributesAsync(); Assert.AreEqual(md5, blob.Properties.ContentMD5); using (MemoryOutputStream downloadedBlob = new MemoryOutputStream()) { await blob.DownloadToStreamAsync(downloadedBlob); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob.UnderlyingStream); } await TestHelper.ExpectedExceptionAsync <ArgumentException>( async() => await blob.OpenWriteAsync(null, null, options, null), "OpenWrite with StoreBlobContentMD5 on an existing page blob should fail"); using (IOutputStream writeStream = await blob.OpenWriteAsync(null)) { Stream blobStream = writeStream.AsStreamForWrite(); blobStream.Seek(buffer.Length / 2, SeekOrigin.Begin); wholeBlob.Seek(buffer.Length / 2, SeekOrigin.Begin); for (int i = 0; i < 2; i++) { blobStream.Write(buffer, 0, buffer.Length); wholeBlob.Write(buffer, 0, buffer.Length); Assert.AreEqual(wholeBlob.Position, blobStream.Position); } wholeBlob.Seek(0, SeekOrigin.End); } await blob.FetchAttributesAsync(); Assert.AreEqual(md5, blob.Properties.ContentMD5); using (MemoryOutputStream downloadedBlob = new MemoryOutputStream()) { options.DisableContentMD5Validation = true; await blob.DownloadToStreamAsync(downloadedBlob, null, options, null); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob.UnderlyingStream); } } } finally { container.DeleteAsync().AsTask().Wait(); } }