public void TplCancellationTest() { MemoryStream blobData = new MemoryStream(GetRandomBuffer(100 * 1024 * 1024)); CloudPageBlob blob = this.testContainer.GetPageBlobReference("blob1"); CancellationTokenSource tokenSource = new CancellationTokenSource(); // Test cancel before starting task tokenSource.Cancel(); Task uploadTask = blob.UploadFromStreamAsync(blobData, tokenSource.Token); TestHelper.ExpectedExceptionTask <StorageException>(uploadTask, "Operation was canceled by user."); tokenSource.Dispose(); tokenSource = new CancellationTokenSource(); Task uploadTask2 = blob.UploadFromStreamAsync(blobData, tokenSource.Token); Thread.Sleep(100); // Should take longer than 0.1 seconds to upload 100 MB // Test cancel after starting task tokenSource.Cancel(); TestHelper.ExpectedExceptionTask <StorageException>(uploadTask2, "Operation was canceled by user."); tokenSource.Dispose(); // Test that task cannot be cancelled after it completes MemoryStream blobData2 = new MemoryStream(GetRandomBuffer(2 * 1024)); tokenSource = new CancellationTokenSource(); Task uploadTask3 = blob.UploadFromStreamAsync(blobData2, tokenSource.Token); while (!uploadTask3.IsCompleted) { Thread.Sleep(1000); } tokenSource.Cancel(); // Should not throw OperationCanceledException because the task was canceled after it completed. uploadTask3.Wait(); Assert.IsFalse(uploadTask3.IsCanceled); Assert.IsTrue(uploadTask3.IsCompleted); Assert.IsFalse(uploadTask3.IsFaulted); Assert.AreEqual(uploadTask3.Status, TaskStatus.RanToCompletion); blobData.Dispose(); }
public async Task PageBlobReadStreamBasicTestAsync() { byte[] buffer = GetRandomBuffer(5 * 1024 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); using (MemoryStream wholeBlob = new MemoryStream(buffer)) { await blob.UploadFromStreamAsync(wholeBlob); } using (MemoryStream wholeBlob = new MemoryStream(buffer)) { using (Stream blobStream = (await blob.OpenReadAsync())) { TestHelper.AssertStreamsAreEqual(wholeBlob, blobStream); } } } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task PageBlobReadStreamSeekTestAsync() { byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamMinimumReadSizeInBytes = 2 * 1024 * 1024; using (MemoryStream wholeBlob = new MemoryStream(buffer)) { await blob.UploadFromStreamAsync(wholeBlob); } OperationContext opContext = new OperationContext(); using (Stream blobStream = await blob.OpenReadAsync(null, null, opContext)) { #if WINDOWS_RT int attempts = await BlobReadStreamSeekTestAsync(blobStream.AsRandomAccessStream(), blob.StreamMinimumReadSizeInBytes, buffer); #else int attempts = await BlobReadStreamSeekTestAsync(blobStream, blob.StreamMinimumReadSizeInBytes, buffer); #endif TestHelper.AssertNAttempts(opContext, attempts); } } finally { container.DeleteIfExistsAsync().Wait(); } }
public void TplDownloadMultipleBlobsTest() { MemoryStream blobData1 = new MemoryStream(GetRandomBuffer(2 * 1024)); MemoryStream blobData2 = new MemoryStream(GetRandomBuffer(2 * 1024)); MemoryStream downloaded1 = new MemoryStream(); MemoryStream downloaded2 = new MemoryStream(); CloudPageBlob blob1 = this.testContainer.GetPageBlobReference("blob1"); CloudPageBlob blob2 = this.testContainer.GetPageBlobReference("blob2"); Task[] uploadTasks = new Task[] { blob1.UploadFromStreamAsync(blobData1), blob2.UploadFromStreamAsync(blobData2) }; Task.WaitAll(uploadTasks); Task[] downloadTasks = new Task[] { blob1.DownloadToStreamAsync(downloaded1), blob2.DownloadToStreamAsync(downloaded2) }; Task.WaitAll(downloadTasks); TestHelper.AssertStreamsAreEqual(blobData1, downloaded1); TestHelper.AssertStreamsAreEqual(blobData2, downloaded2); blobData1.Dispose(); blobData2.Dispose(); downloaded1.Dispose(); downloaded2.Dispose(); }
public async Task BlobSeekTestAsync() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); using (MemoryStream srcStream = new MemoryStream(buffer)) { await blob.UploadFromStreamAsync(srcStream, null, null, null); using (Stream blobStream = await blob.OpenReadAsync()) { Stream blobStreamForRead = blobStream; blobStreamForRead.Seek(2048, 0); byte[] buff = new byte[100]; int numRead = await blobStreamForRead.ReadAsync(buff, 0, 100); Assert.AreEqual(numRead, 0); } } } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task BlobOpenReadTestAsync() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); using (MemoryStream srcStream = new MemoryStream(buffer)) { await blob.UploadFromStreamAsync(srcStream); using (Stream dstStreamForRead = (await blob.OpenReadAsync())) { TestHelper.AssertStreamsAreEqual(srcStream, dstStreamForRead); } } } finally { container.DeleteIfExistsAsync().Wait(); } }
public static void UploadTextTask(CloudBlob blob, string text, Encoding encoding, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null) { byte[] textAsBytes = encoding.GetBytes(text); using (MemoryStream stream = new MemoryStream()) { stream.Write(textAsBytes, 0, textAsBytes.Length); if (blob.BlobType == BlobType.PageBlob) { int lastPageSize = (int)(stream.Length % 512); if (lastPageSize != 0) { byte[] padding = new byte[512 - lastPageSize]; stream.Write(padding, 0, padding.Length); } } stream.Seek(0, SeekOrigin.Begin); blob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2; try { if (blob.BlobType == BlobType.AppendBlob) { CloudAppendBlob blob1 = blob as CloudAppendBlob; blob1.CreateOrReplaceAsync().Wait(); blob1.AppendBlock(stream, null); } else if (blob.BlobType == BlobType.PageBlob) { CloudPageBlob pageBlob = blob as CloudPageBlob; pageBlob.UploadFromStreamAsync(stream, accessCondition, options, operationContext).Wait(); } else { CloudBlockBlob blockBlob = blob as CloudBlockBlob; blockBlob.UploadFromStreamAsync(stream, accessCondition, options, operationContext).Wait(); } } catch (AggregateException ex) { if (ex.InnerException != null) { throw ex.InnerException; } throw; } } }
public static async Task UploadTextAsync(CloudBlob blob, string text, Encoding encoding, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null) { byte[] textAsBytes = encoding.GetBytes(text); using (MemoryStream stream = new MemoryStream()) { await stream.WriteAsync(textAsBytes, 0, textAsBytes.Length); if (blob.BlobType == BlobType.PageBlob) { int lastPageSize = (int)(stream.Length % 512); if (lastPageSize != 0) { byte[] padding = new byte[512 - lastPageSize]; await stream.WriteAsync(padding, 0, padding.Length); } } stream.Seek(0, SeekOrigin.Begin); blob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2; if (blob.BlobType == BlobType.AppendBlob) { CloudAppendBlob blob1 = blob as CloudAppendBlob; await blob1.CreateOrReplaceAsync(); #if !FACADE_NETCORE await blob1.AppendBlockAsync(stream, null); #else await blob1.AppendBlockAsync(stream, null, null, null, null, CancellationToken.None); #endif } else if (blob.BlobType == BlobType.PageBlob) { CloudPageBlob pageBlob = blob as CloudPageBlob; await pageBlob.UploadFromStreamAsync(stream, accessCondition, options, operationContext); } else { CloudBlockBlob blockBlob = blob as CloudBlockBlob; await blockBlob.UploadFromStreamAsync(stream, accessCondition, options, operationContext); } } }
public async Task BlobWriteWhenOpenReadAsync() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); using (MemoryStream srcStream = new MemoryStream(buffer)) { await blob.UploadFromStreamAsync(srcStream); bool thrown = false; byte[] testBuffer = new byte[2048]; using (Stream blobStream = await blob.OpenReadAsync()) { Stream blobStreamForRead = blobStream; try { await blobStreamForRead.WriteAsync(testBuffer, 0, 2048); } catch (NotSupportedException) { thrown = true; } Assert.IsTrue(thrown); } } } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task StoreBlobContentMD5TestAsync() { BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions() { StoreBlobContentMD5 = false, }; BlobRequestOptions optionsWithMD5 = new BlobRequestOptions() { StoreBlobContentMD5 = true, }; CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob1 = container.GetBlockBlobReference("blob1"); using (Stream stream = new NonSeekableMemoryStream()) { await blob1.UploadFromStreamAsync(stream, null, optionsWithMD5, null); } await blob1.FetchAttributesAsync(); Assert.IsNotNull(blob1.Properties.ContentMD5); blob1 = container.GetBlockBlobReference("blob2"); using (Stream stream = new NonSeekableMemoryStream()) { await blob1.UploadFromStreamAsync(stream, null, optionsWithNoMD5, null); } await blob1.FetchAttributesAsync(); Assert.IsNull(blob1.Properties.ContentMD5); blob1 = container.GetBlockBlobReference("blob3"); using (Stream stream = new NonSeekableMemoryStream()) { await blob1.UploadFromStreamAsync(stream); } await blob1.FetchAttributesAsync(); Assert.IsNotNull(blob1.Properties.ContentMD5); CloudPageBlob blob2 = container.GetPageBlobReference("blob4"); blob2 = container.GetPageBlobReference("blob4"); using (Stream stream = new MemoryStream()) { await blob2.UploadFromStreamAsync(stream, null, optionsWithMD5, null); } await blob2.FetchAttributesAsync(); Assert.IsNotNull(blob2.Properties.ContentMD5); blob2 = container.GetPageBlobReference("blob5"); using (Stream stream = new MemoryStream()) { await blob2.UploadFromStreamAsync(stream, null, optionsWithNoMD5, null); } await blob2.FetchAttributesAsync(); Assert.IsNull(blob2.Properties.ContentMD5); blob2 = container.GetPageBlobReference("blob6"); using (Stream stream = new MemoryStream()) { await blob2.UploadFromStreamAsync(stream); } await blob2.FetchAttributesAsync(); Assert.IsNull(blob2.Properties.ContentMD5); } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task DisableContentMD5ValidationTestAsync() { byte[] buffer = new byte[1024]; Random random = new Random(); random.NextBytes(buffer); BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions() { DisableContentMD5Validation = true, StoreBlobContentMD5 = true, }; BlobRequestOptions optionsWithMD5 = new BlobRequestOptions() { DisableContentMD5Validation = false, StoreBlobContentMD5 = true, }; CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); using (Stream stream = new NonSeekableMemoryStream(buffer)) { await blockBlob.UploadFromStreamAsync(stream, null, optionsWithMD5, null); } using (Stream stream = new MemoryStream()) { await blockBlob.DownloadToStreamAsync(stream, null, optionsWithMD5, null); await blockBlob.DownloadToStreamAsync(stream, null, optionsWithNoMD5, null); using (Stream blobStream = await blockBlob.OpenReadAsync(null, optionsWithMD5, null)) { Stream blobStreamForRead = blobStream; int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } using (Stream blobStream = await blockBlob.OpenReadAsync(null, optionsWithNoMD5, null)) { Stream blobStreamForRead = blobStream; int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } blockBlob.Properties.ContentMD5 = "MDAwMDAwMDA="; await blockBlob.SetPropertiesAsync(); OperationContext opContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await blockBlob.DownloadToStreamAsync(stream, null, optionsWithMD5, opContext), opContext, "Downloading a blob with invalid MD5 should fail", HttpStatusCode.OK); await blockBlob.DownloadToStreamAsync(stream, null, optionsWithNoMD5, null); using (Stream blobStream = await blockBlob.OpenReadAsync(null, optionsWithMD5, null)) { Stream blobStreamForRead = blobStream; TestHelper.ExpectedException <IOException>( () => { int read; do { read = blobStreamForRead.Read(buffer, 0, buffer.Length); }while (read > 0); }, "Downloading a blob with invalid MD5 should fail"); } using (Stream blobStream = await blockBlob.OpenReadAsync(null, optionsWithNoMD5, null)) { Stream blobStreamForRead = blobStream; int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } } CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); using (Stream stream = new MemoryStream(buffer)) { await pageBlob.UploadFromStreamAsync(stream, null, optionsWithMD5, null); } using (Stream stream = new MemoryStream()) { await pageBlob.DownloadToStreamAsync(stream, null, optionsWithMD5, null); await pageBlob.DownloadToStreamAsync(stream, null, optionsWithNoMD5, null); using (Stream blobStream = await pageBlob.OpenReadAsync(null, optionsWithMD5, null)) { Stream blobStreamForRead = blobStream; int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } using (Stream blobStream = await pageBlob.OpenReadAsync(null, optionsWithNoMD5, null)) { Stream blobStreamForRead = blobStream; int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } pageBlob.Properties.ContentMD5 = "MDAwMDAwMDA="; await pageBlob.SetPropertiesAsync(); OperationContext opContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async() => await pageBlob.DownloadToStreamAsync(stream, null, optionsWithMD5, opContext), opContext, "Downloading a blob with invalid MD5 should fail", HttpStatusCode.OK); await pageBlob.DownloadToStreamAsync(stream, null, optionsWithNoMD5, null); using (Stream blobStream = await pageBlob.OpenReadAsync(null, optionsWithMD5, null)) { Stream blobStreamForRead = blobStream; TestHelper.ExpectedException <IOException>( () => { int read; do { read = blobStreamForRead.Read(buffer, 0, buffer.Length); }while (read > 0); }, "Downloading a blob with invalid MD5 should fail"); } using (Stream blobStream = await pageBlob.OpenReadAsync(null, optionsWithNoMD5, null)) { Stream blobStreamForRead = blobStream; int read; do { read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length); }while (read > 0); } } } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task CloudBlobClientMaximumExecutionTimeoutAsync() { CloudBlobClient blobClient = GenerateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(Guid.NewGuid().ToString("N")); byte[] buffer = BlobTestBase.GetRandomBuffer(80 * 1024 * 1024); try { await container.CreateAsync(); blobClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(5); blobClient.DefaultRequestOptions.SingleBlobUploadThresholdInBytes = 2 * 1024 * 1024; CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); blockBlob.StreamWriteSizeInBytes = 1 * 1024 * 1024; using (MemoryStream ms = new MemoryStream(buffer)) { try { await blockBlob.UploadFromStreamAsync(ms); Assert.Fail(); } catch (AggregateException ex) { #if !FACADE_NETCORE Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).ExceptionInfo.Message); #else Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).Exception.Message); #endif } catch (TaskCanceledException) { } } CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); pageBlob.StreamWriteSizeInBytes = 1 * 1024 * 1024; using (MemoryStream ms = new MemoryStream(buffer)) { try { await pageBlob.UploadFromStreamAsync(ms); Assert.Fail(); } catch (AggregateException ex) { #if !FACADE_NETCORE Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).ExceptionInfo.Message); #else Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).Exception.Message); #endif } catch (TaskCanceledException) { } } } finally { blobClient.DefaultRequestOptions.MaximumExecutionTime = null; container.DeleteIfExistsAsync().Wait(); } }
public async Task PageBlobReadLockToETagTestAsync() { byte[] outBuffer = new byte[1 * 1024 * 1024]; byte[] buffer = GetRandomBuffer(2 * outBuffer.Length); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); blob.StreamMinimumReadSizeInBytes = outBuffer.Length; using (MemoryStream wholeBlob = new MemoryStream(buffer)) { await blob.UploadFromStreamAsync(wholeBlob); } OperationContext opContext = new OperationContext(); using (Stream blobStream = await blob.OpenReadAsync(null, null, opContext)) { Stream blobStreamForRead = blobStream; await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length); await blob.SetMetadataAsync(); await TestHelper.ExpectedExceptionAsync( async() => await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length), opContext, "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } opContext = new OperationContext(); using (Stream blobStream = await blob.OpenReadAsync(null, null, opContext)) { Stream blobStreamForRead = blobStream; long length = blobStreamForRead.Length; await blob.SetMetadataAsync(); await TestHelper.ExpectedExceptionAsync( async() => await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length), opContext, "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } opContext = new OperationContext(); AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1))); await blob.SetMetadataAsync(); await TestHelper.ExpectedExceptionAsync( async() => await blob.OpenReadAsync(accessCondition, null, opContext), opContext, "Blob read stream should fail if blob is modified during read", HttpStatusCode.PreconditionFailed); } finally { container.DeleteIfExistsAsync().Wait(); } }