public async Task CloudBlockBlobDownloadBlockListAsync() { byte[] buffer = GetRandomBuffer(1024); List <string> blocks = GetBlockIdList(3); List <string> extraBlocks = GetBlockIdList(2); CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blob = container.GetBlockBlobReference("blob1"); foreach (string block in blocks) { using (MemoryStream memoryStream = new MemoryStream(buffer)) { await blob.PutBlockAsync(block, memoryStream.AsInputStream(), null); } } await blob.PutBlockListAsync(blocks); foreach (string block in extraBlocks) { using (MemoryStream memoryStream = new MemoryStream(buffer)) { await blob.PutBlockAsync(block, memoryStream.AsInputStream(), null); } } CloudBlockBlob blob2 = container.GetBlockBlobReference("blob1"); await blob2.FetchAttributesAsync(); Assert.AreEqual(1024 * blocks.Count, blob2.Properties.Length); IEnumerable <ListBlockItem> blockList = await blob2.DownloadBlockListAsync(); foreach (ListBlockItem blockItem in blockList) { Assert.IsTrue(blockItem.Committed); Assert.IsTrue(blocks.Remove(blockItem.Name)); } Assert.AreEqual(0, blocks.Count); blockList = await blob2.DownloadBlockListAsync(BlockListingFilter.Uncommitted, null, null, null); foreach (ListBlockItem blockItem in blockList) { Assert.IsFalse(blockItem.Committed); Assert.IsTrue(extraBlocks.Remove(blockItem.Name)); } Assert.AreEqual(0, extraBlocks.Count); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
public async Task CloudBlockBlobMethodsOnPageBlobAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); List <string> blobs = await CreateBlobsAsync(container, 1, BlobType.PageBlob); CloudBlockBlob blob = container.GetBlockBlobReference(blobs.First()); List <string> blockList = GetBlockIdList(1); OperationContext operationContext = new OperationContext(); byte[] buffer = new byte[1]; using (MemoryStream stream = new MemoryStream(buffer)) { await TestHelper.ExpectedExceptionAsync( async() => await blob.PutBlockAsync(blockList.First(), stream.AsInputStream(), null, null, null, operationContext), operationContext, "Block operations should fail on page blobs", HttpStatusCode.Conflict, "InvalidBlobType"); } await TestHelper.ExpectedExceptionAsync( async() => await blob.PutBlockListAsync(blockList, null, null, operationContext), operationContext, "Block operations should fail on page blobs", HttpStatusCode.Conflict, "InvalidBlobType"); await TestHelper.ExpectedExceptionAsync( async() => await blob.DownloadBlockListAsync(BlockListingFilter.Committed, null, null, operationContext), operationContext, "Block operations should fail on page blobs", HttpStatusCode.Conflict, "InvalidBlobType"); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
/// <summary> /// Reads the blob's block list, and indicates whether the blob has been committed. /// </summary> /// <param name="blob">A CloudBlockBlob object.</param> /// <returns>A Task object.</returns> private static async Task ReadBlockListAsync(CloudBlockBlob blob) { // Get the blob's block list. foreach (var listBlockItem in await blob.DownloadBlockListAsync(BlockListingFilter.All, null, null, null)) { if (listBlockItem.Committed) { Console.WriteLine( "Block {0} has been committed to block list. Block length = {1}", listBlockItem.Name, listBlockItem.Length); } else { Console.WriteLine( "Block {0} is uncommitted. Block length = {1}", listBlockItem.Name, listBlockItem.Length); } } Console.WriteLine(); }
private static async Task AppendToBlob(CloudBlockBlob blockBlob, string content, string md5) { var leaseId = await blockBlob.AcquireLeaseAsync(TimeSpan.FromSeconds(30), null); var access = AccessCondition.GenerateLeaseCondition(leaseId); var options = new BlobRequestOptions(); var context = new OperationContext(); try { var blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content))) { stream.Position = 0; await blockBlob.PutBlockAsync(blockId, stream, md5, access, options, context); } var blockList = await blockBlob.DownloadBlockListAsync(BlockListingFilter.Committed, access, options, context); var blockIds = blockList.Select(x => x.Name).ToList(); blockIds.Add(blockId); await blockBlob.PutBlockListAsync(blockIds, access, options, context); } catch(Exception ex) { //TODO - logging } finally { await blockBlob.ReleaseLeaseAsync(access); } }
/// <summary> /// Test block blob reads, expecting success. /// </summary> /// <param name="testBlob">The block blob.</param> /// <param name="testAccessCondition">The access condition to use.</param> private async Task BlockBlobReadExpectLeaseSuccessAsync(CloudBlockBlob testBlob, AccessCondition testAccessCondition) { await testBlob.DownloadBlockListAsync(BlockListingFilter.Committed, testAccessCondition, null /* options */, null); }
/// <summary> /// Test block blob reads, expecting lease failure. /// </summary> /// <param name="testBlob">The block blob.</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 BlockBlobReadExpectLeaseFailureAsync(CloudBlockBlob testBlob, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description) { OperationContext operationContext = new OperationContext(); await TestHelper.ExpectedExceptionAsync( async () => await testBlob.DownloadBlockListAsync(BlockListingFilter.Committed, testAccessCondition, null /* options */, operationContext), operationContext, description + "(Download Block List)", expectedStatusCode, expectedErrorCode); }