public async Task BlockBlobWriteStreamFlushTestAsync() { byte[] buffer = GetRandomBuffer(512 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); blob.StreamWriteSizeInBytes = 1 * 1024 * 1024; using (MemoryStream wholeBlob = new MemoryStream()) { OperationContext opContext = new OperationContext(); using (ICloudBlobStream blobStream = await blob.OpenWriteAsync(null, null, opContext)) { for (int i = 0; i < 3; i++) { await blobStream.WriteAsync(buffer.AsBuffer()); await wholeBlob.WriteAsync(buffer, 0, buffer.Length); } Assert.AreEqual(1, opContext.RequestResults.Count); await blobStream.FlushAsync(); Assert.AreEqual(2, opContext.RequestResults.Count); await blobStream.FlushAsync(); Assert.AreEqual(2, opContext.RequestResults.Count); await blobStream.WriteAsync(buffer.AsBuffer()); await wholeBlob.WriteAsync(buffer, 0, buffer.Length); Assert.AreEqual(2, opContext.RequestResults.Count); await blobStream.CommitAsync(); Assert.AreEqual(4, opContext.RequestResults.Count); } Assert.AreEqual(4, opContext.RequestResults.Count); using (MemoryStream downloadedBlob = new MemoryStream()) { await blob.DownloadToStreamAsync(downloadedBlob.AsOutputStream()); TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob); } } } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task BlobWriteStreamOpenAndCloseAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); // Block blob tests CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); using (Stream writeStream = await blockBlob.OpenWriteAsync()) { } CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(blockBlob.Name); await blockBlob2.FetchAttributesAsync(); Assert.AreEqual(0, blockBlob2.Properties.Length); Assert.AreEqual(BlobType.BlockBlob, blockBlob2.Properties.BlobType); // Page blob tests CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); OperationContext opContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await pageBlob.OpenWriteAsync(null, null, null, opContext), opContext, "Opening a page blob stream with no size should fail on a blob that does not exist", HttpStatusCode.NotFound); using (Stream writeStream = await pageBlob.OpenWriteAsync(1024)) { } using (Stream writeStream = await pageBlob.OpenWriteAsync(null)) { } CloudPageBlob pageBlob2 = container.GetPageBlobReference(pageBlob.Name); await pageBlob2.FetchAttributesAsync(); Assert.AreEqual(1024, pageBlob2.Properties.Length); Assert.AreEqual(BlobType.PageBlob, pageBlob2.Properties.BlobType); // Append blob test CloudAppendBlob appendBlob = container.GetAppendBlobReference("blob3"); using (Stream writeStream = await appendBlob.OpenWriteAsync(true)) { } CloudAppendBlob appendBlob2 = container.GetAppendBlobReference(appendBlob.Name); await appendBlob2.FetchAttributesAsync(); Assert.AreEqual(0, appendBlob2.Properties.Length); Assert.AreEqual(BlobType.AppendBlob, appendBlob2.Properties.BlobType); } finally { container.DeleteAsync().Wait(); } }
public async Task BlockBlobWriteStreamBasicTestAsync() { byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash(); CloudBlobClient blobClient = GenerateCloudBlobClient(); blobClient.DefaultRequestOptions.ParallelOperationThreadCount = 2; string name = GetRandomContainerName(); CloudBlobContainer container = blobClient.GetContainerReference(name); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); using (MemoryStream wholeBlob = new MemoryStream()) { BlobRequestOptions options = new BlobRequestOptions() { StoreBlobContentMD5 = true, }; using (IOutputStream writeStream = await blob.OpenWriteAsync(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()); } await blobStream.FlushAsync(); } 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); } } } finally { container.DeleteAsync().AsTask().Wait(); } }
public async Task CloudBlockBlobSnapshotAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); await CreateForTestAsync(blob, 2, 1024); CloudBlockBlob snapshot1 = await blob.CreateSnapshotAsync(); Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); CloudBlockBlob snapshot2 = await blob.CreateSnapshotAsync(); Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); await snapshot1.FetchAttributesAsync(); await snapshot2.FetchAttributesAsync(); await blob.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudBlockBlob snapshotCopy = container.GetBlockBlobReference("blob2"); await snapshotCopy.StartCopyFromBlobAsync(TestHelper.Defiddler(snapshot1.Uri)); await WaitForCopyAsync(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); await TestHelper.ExpectedExceptionAsync <InvalidOperationException>( async() => await snapshot1.OpenWriteAsync(), "Trying to write to a blob snapshot should fail"); BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null); List <IListBlobItem> blobs = resultSegment.Results.ToList(); Assert.AreEqual(4, blobs.Count); AssertAreEqual(snapshot1, (ICloudBlob)blobs[0]); AssertAreEqual(snapshot2, (ICloudBlob)blobs[1]); AssertAreEqual(blob, (ICloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (ICloudBlob)blobs[3]); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task BlockBlobWriteStreamBasicTestAsync() { byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); CloudBlobClient blobClient = GenerateCloudBlobClient(); blobClient.ParallelOperationThreadCount = 2; string name = GetRandomContainerName(); CloudBlobContainer container = blobClient.GetContainerReference(name); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); using (MemoryStream wholeBlob = new MemoryStream()) { using (Stream writeStream = await blob.OpenWriteAsync()) { 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); } } } finally { container.DeleteAsync().Wait(); } }
public async Task BlockBlobWriteStreamSeekTestAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); using (IOutputStream writeStream = await blob.OpenWriteAsync()) { Stream blobStream = writeStream.AsStreamForWrite(); TestHelper.ExpectedException <NotSupportedException>( () => blobStream.Seek(1, SeekOrigin.Begin), "Block blob write stream should not be seekable"); } } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudBlobClientMaximumExecutionTimeoutShouldNotBeHonoredForStreamsAsync() { CloudBlobClient blobClient = GenerateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(Guid.NewGuid().ToString("N")); byte[] buffer = BlobTestBase.GetRandomBuffer(1024 * 1024); try { await container.CreateAsync(); blobClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(30); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); blockBlob.StreamWriteSizeInBytes = 1024 * 1024; blockBlob.StreamMinimumReadSizeInBytes = 1024 * 1024; pageBlob.StreamWriteSizeInBytes = 1024 * 1024; pageBlob.StreamMinimumReadSizeInBytes = 1024 * 1024; using (var bos = await blockBlob.OpenWriteAsync()) { DateTime start = DateTime.Now; for (int i = 0; i < 7; i++) { await bos.WriteAsync(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last write int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { await Task.Delay(msRemaining); } await bos.WriteAsync(buffer, 0, buffer.Length); await bos.CommitAsync(); } using (Stream bis = (await blockBlob.OpenReadAsync())) { DateTime start = DateTime.Now; int total = 0; while (total < 7 * 1024 * 1024) { total += await bis.ReadAsync(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last read int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { await Task.Delay(msRemaining); } while (true) { int count = await bis.ReadAsync(buffer, 0, buffer.Length); total += count; if (count == 0) { break; } } } using (var bos = await pageBlob.OpenWriteAsync(8 * 1024 * 1024)) { DateTime start = DateTime.Now; for (int i = 0; i < 7; i++) { await bos.WriteAsync(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last write int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { await Task.Delay(msRemaining); } await bos.WriteAsync(buffer, 0, buffer.Length); await bos.CommitAsync(); } using (Stream bis = (await pageBlob.OpenReadAsync())) { DateTime start = DateTime.Now; int total = 0; while (total < 7 * 1024 * 1024) { total += await bis.ReadAsync(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last read int msRemaining = (int)(blobClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { await Task.Delay(msRemaining); } while (true) { int count = await bis.ReadAsync(buffer, 0, buffer.Length); total += count; if (count == 0) { break; } } } } finally { blobClient.DefaultRequestOptions.MaximumExecutionTime = null; container.DeleteIfExistsAsync().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(); } }
/// <summary> /// Test blob writing, expecting success. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="sourceBlob">A blob to use as the source of a copy.</param> /// <param name="testAccessCondition">The access condition to use.</param> private async Task BlobWriteExpectLeaseSuccessAsync(CloudBlockBlob testBlob, CloudBlob sourceBlob, AccessCondition testAccessCondition) { await testBlob.SetMetadataAsync(testAccessCondition, null /* options */, null); await testBlob.SetPropertiesAsync(testAccessCondition, null /* options */, null); await UploadTextAsync(testBlob, "No Problem", Encoding.UTF8, testAccessCondition, null /* options */, null); await testBlob.StartCopyAsync(TestHelper.Defiddler(sourceBlob.Uri), null /* source access condition */, testAccessCondition, null /* options */, null); while (testBlob.CopyState.Status == CopyStatus.Pending) { await Task.Delay(1000); await testBlob.FetchAttributesAsync(); } var writeStream = await testBlob.OpenWriteAsync(testAccessCondition, null /* options */, null); Stream stream = writeStream.AsStreamForWrite(); stream.WriteByte(0); await stream.FlushAsync(); await testBlob.DeleteAsync(DeleteSnapshotsOption.None, testAccessCondition, null /* options */, null); }
/// <summary> /// Test blob creation, expecting lease failure. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="sourceBlob">A blob to use as the source of a copy.</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 BlobCreateExpectLeaseFailureAsync(CloudBlockBlob testBlob, CloudBlockBlob sourceBlob, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description) { OperationContext operationContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async () => await UploadTextAsync(testBlob, "No Dice", Encoding.UTF8, testAccessCondition, null /* options */, operationContext), operationContext, description + " (Upload Text)", expectedStatusCode, expectedErrorCode); await TestHelper.ExpectedExceptionAsync( async () => await testBlob.StartCopyAsync(TestHelper.Defiddler(sourceBlob.Uri), null /* source access condition */, testAccessCondition, null /* options */, operationContext), operationContext, description + " (Copy From)", expectedStatusCode, expectedErrorCode); var writeStream = await testBlob.OpenWriteAsync(testAccessCondition, null /* options */, operationContext); Stream stream = writeStream.AsStreamForWrite(); await TestHelper.ExpectedExceptionAsync( async () => { stream.WriteByte(0); await stream.FlushAsync(); }, operationContext, description + " (Write Stream)", expectedStatusCode, expectedErrorCode); }
/// <summary> /// Test blob writing, expecting success. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="sourceBlob">A blob to use as the source of a copy.</param> /// <param name="testAccessCondition">The access condition to use.</param> private void BlobWriteExpectLeaseSuccessTask(CloudBlockBlob testBlob, CloudBlob sourceBlob, AccessCondition testAccessCondition) { testBlob.SetMetadataAsync(testAccessCondition, null /* options */, new OperationContext()).Wait(); testBlob.SetPropertiesAsync(testAccessCondition, null /* options */, new OperationContext()).Wait(); UploadTextTask(testBlob, "No Problem", Encoding.UTF8, testAccessCondition, null /* options */, new OperationContext()); testBlob.StartCopyAsync( TestHelper.Defiddler(sourceBlob.Uri), null /* source access condition */, testAccessCondition, null /* options */, new OperationContext()).Wait(); while (testBlob.CopyState.Status == CopyStatus.Pending) { Thread.Sleep(1000); testBlob.FetchAttributesAsync().Wait(); } Stream stream = testBlob.OpenWriteAsync(testAccessCondition, null /* options */, new OperationContext()).Result; stream.WriteByte(0); stream.Flush(); testBlob.DeleteAsync(DeleteSnapshotsOption.None, testAccessCondition, null /* options */, new OperationContext()); }
/// <summary> /// Test blob creation, expecting lease failure. /// </summary> /// <param name="testBlob">The blob to test.</param> /// <param name="sourceBlob">A blob to use as the source of a copy.</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 BlobCreateExpectLeaseFailureTask(CloudBlockBlob testBlob, CloudBlockBlob sourceBlob, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description) { TestHelper.ExpectedException( () => UploadTextTask(testBlob, "No Dice", Encoding.UTF8, testAccessCondition, null /* options */), description + " (Upload Text)", expectedStatusCode, expectedErrorCode); TestHelper.ExpectedExceptionTask( testBlob.StartCopyAsync(TestHelper.Defiddler(sourceBlob.Uri), null /* source access condition */, testAccessCondition, null /* options */, new OperationContext()), description + " (Copy From)", expectedStatusCode, expectedErrorCode); Stream stream = testBlob.OpenWriteAsync(testAccessCondition, null /* options */, new OperationContext()).Result; TestHelper.ExpectedException( () => { stream.WriteByte(0); stream.Flush(); }, description + " (Write Stream)", expectedStatusCode, expectedErrorCode); }