public async Task LargeDownloadVerifyRangeSizeRestrictions() { string inputFileName = Path.GetTempFileName(); string outputFileName = Path.GetTempFileName(); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("largeblob1"); await blob.UploadTextAsync("Tent"); try { await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 16 *Constants.MB + 3, CancellationToken.None); Assert.Fail("Expected a failure"); } catch (ArgumentException) {} try { await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 2 *Constants.MB, CancellationToken.None); Assert.Fail("Expected a failure"); } catch (ArgumentOutOfRangeException) {} try { BlobRequestOptions options = new BlobRequestOptions() { ChecksumOptions = new ChecksumOptions { UseTransactionalMD5 = true, UseTransactionalCRC64 = true } }; await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 16 *Constants.MB, 0, null, null, options, null, CancellationToken.None); Assert.Fail("Expected a failure"); } catch (ArgumentException) {} blob.Delete(); } finally { container.DeleteIfExists(); File.Delete(inputFileName); File.Delete(outputFileName); } }
public async Task LargeDownloadVerifyCancellationTest() { string inputFileName = Path.GetTempFileName(); string outputFileName = Path.GetTempFileName(); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("largeblob1"); long bufferSize = 2 * Constants.KB; long offSet = 0; using (FileStream file = new FileStream(inputFileName, FileMode.Create, FileAccess.Write)) { while (offSet < 200 * Constants.MB) { byte[] buffer = GetRandomBuffer(bufferSize); await file.WriteAsync(buffer, 0, buffer.Length); offSet += bufferSize; } } BlobRequestOptions options = new BlobRequestOptions(); options.ParallelOperationThreadCount = 16; await blob.UploadFromFileAsync(inputFileName, null, options, null); CancellationTokenSource cts = new CancellationTokenSource(); try { Task downloadTask = blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, 16, 16 * Constants.MB, cts.Token); await Task.Delay(1000); cts.Cancel(); await downloadTask; Assert.Fail("Expected a cancellation exception to be thrown"); } catch (StorageException ex) { Exception s = ex.InnerException; } blob.Delete(); } finally { container.DeleteIfExists(); File.Delete(inputFileName); File.Delete(outputFileName); } }
public async Task LargeDownloadToFileTest() { string inputFileName = Path.GetTempFileName(); string outputFileName = Path.GetTempFileName(); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("largeblob1"); long bufferSize = 2 * Constants.KB; long offSet = 0; using (FileStream file = new FileStream(inputFileName, FileMode.Create, FileAccess.Write)) { while (offSet < 500 * Constants.MB) { byte[] buffer = GetRandomBuffer(bufferSize); await file.WriteAsync(buffer, 0, buffer.Length); offSet += bufferSize; } } BlobRequestOptions options = new BlobRequestOptions(); options.ParallelOperationThreadCount = 16; await blob.UploadFromFileAsync(inputFileName, null, options, null); #region sample_DownloadToFileParallel // When calling the DownloadToFileParallelAsync API, // the parallelIOCount variable represents how many ranges can be downloaded concurrently. If the // parallel I/O count reaches this threshold, no more further requests are made until one range completes. // The rangeSizeInBytes represents the size of each individual range that is being dowloaded in parallel. // Passing a cancellation token is advised since for certain network errors, this code will continue to retry indefintitely. int parallelIOCount = 16; long rangeSizeInBytes = 16 * Constants.MB; CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); await blob.DownloadToFileParallelAsync(outputFileName, FileMode.Create, parallelIOCount, rangeSizeInBytes, cancellationTokenSource.Token); #endregion await blob.DeleteAsync(); Assert.IsTrue(FilesAreEqual(inputFileName, outputFileName)); } finally { container.DeleteIfExists(); File.Delete(inputFileName); File.Delete(outputFileName); } }
public async Task LargeDownloadRangeToFileTest() { string inputFileName = Path.GetTempFileName(); string outputFileName = Path.GetTempFileName(); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("largeblob1"); long bufferSize = 2 * Constants.KB; long offSet = 0; using (FileStream file = new FileStream(inputFileName, FileMode.Create, FileAccess.Write)) { while (offSet < 500 * Constants.MB) { byte[] buffer = GetRandomBuffer(bufferSize); await file.WriteAsync(buffer, 0, buffer.Length); offSet += bufferSize; } } BlobRequestOptions options = new BlobRequestOptions(); options.ParallelOperationThreadCount = 16; await blob.UploadFromFileAsync(inputFileName, null, options, null); long blobOffset = 3 * Constants.KB; await blob.DownloadToFileParallelAsync( outputFileName, FileMode.Create, 16, 16 *Constants.MB, blobOffset, 200 *Constants.MB, null, null, null, CancellationToken.None); await blob.DeleteAsync(); Assert.IsTrue(FilesAreEqual(inputFileName, outputFileName, blobOffset)); } finally { container.DeleteIfExists(); File.Delete(inputFileName); File.Delete(outputFileName); } }
public async Task ParallelDownloadToEmptyFileTest() { string inputFileName = Path.GetTempFileName(); string outputFileName = Path.GetTempPath() + "output.tmp"; CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("largeblob1"); FileStream fs = File.Create(inputFileName); fs.Close(); BlobRequestOptions options = new BlobRequestOptions(); options.ParallelOperationThreadCount = 16; await blob.UploadFromFileAsync(inputFileName, null, options, null); Assert.IsFalse(File.Exists(outputFileName)); await blob.DownloadToFileParallelAsync( outputFileName, FileMode.Create, 16, null); Assert.IsTrue(File.Exists(outputFileName)); await blob.DeleteAsync(); } finally { container.DeleteIfExists(); File.Delete(inputFileName); File.Delete(outputFileName); } }