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 CloudAppendBlobSnapshotAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob blob = container.GetAppendBlobReference("blob1"); await blob.CreateOrReplaceAsync(); await blob.AppendBlockAsync(originalData, null); Assert.IsFalse(blob.IsSnapshot); Assert.IsNull(blob.SnapshotTime, "Root blob has SnapshotTime set"); Assert.IsFalse(blob.SnapshotQualifiedUri.Query.Contains("snapshot")); Assert.AreEqual(blob.Uri, blob.SnapshotQualifiedUri); CloudAppendBlob snapshot1 = await blob.CreateSnapshotAsync(); Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsTrue(snapshot1.IsSnapshot); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); Assert.AreEqual(blob.Uri, snapshot1.Uri); Assert.AreNotEqual(blob.SnapshotQualifiedUri, snapshot1.SnapshotQualifiedUri); Assert.AreNotEqual(snapshot1.Uri, snapshot1.SnapshotQualifiedUri); Assert.IsTrue(snapshot1.SnapshotQualifiedUri.Query.Contains("snapshot")); CloudAppendBlob 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); CloudAppendBlob snapshot1Clone = new CloudAppendBlob(new Uri(blob.Uri + "?snapshot=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshot1Clone.SnapshotTime, "Snapshot clone does not have SnapshotTime set"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshot1Clone.SnapshotTime.Value); await snapshot1Clone.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties, false); CloudAppendBlob snapshotCopy = container.GetAppendBlobReference("blob2"); await snapshotCopy.StartCopyAsync(TestHelper.Defiddler(snapshot1.Uri)); await WaitForCopyAsync(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); using (Stream snapshotStream = (await snapshot1.OpenReadAsync())) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } await blob.CreateOrReplaceAsync(); using (Stream snapshotStream = (await snapshot1.OpenReadAsync())) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } 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, (CloudBlob)blobs[0]); AssertAreEqual(snapshot2, (CloudBlob)blobs[1]); AssertAreEqual(blob, (CloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (CloudBlob)blobs[3]); } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task CloudBlobSnapshotAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); await appendBlob.CreateOrReplaceAsync(); await appendBlob.AppendBlockAsync(originalData, null); CloudBlob blob = container.GetBlobReference(BlobName); await blob.FetchAttributesAsync(); Assert.IsFalse(blob.IsSnapshot); Assert.IsNull(blob.SnapshotTime, "Root blob has SnapshotTime set"); Assert.IsFalse(blob.SnapshotQualifiedUri.Query.Contains("snapshot")); Assert.AreEqual(blob.Uri, blob.SnapshotQualifiedUri); CloudBlob snapshot1 = await blob.SnapshotAsync(); Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag); Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified); Assert.IsTrue(snapshot1.IsSnapshot); Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set"); Assert.AreEqual(blob.Uri, snapshot1.Uri); Assert.AreNotEqual(blob.SnapshotQualifiedUri, snapshot1.SnapshotQualifiedUri); Assert.AreNotEqual(snapshot1.Uri, snapshot1.SnapshotQualifiedUri); Assert.IsTrue(snapshot1.SnapshotQualifiedUri.Query.Contains("snapshot")); CloudBlob snapshot2 = await blob.SnapshotAsync(); Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value); await snapshot1.FetchAttributesAsync(); await snapshot2.FetchAttributesAsync(); await blob.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, blob.Properties); CloudBlob snapshot1Clone = new CloudBlob(new Uri(blob.Uri + "?snapshot=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshot1Clone.SnapshotTime, "Snapshot clone does not have SnapshotTime set"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshot1Clone.SnapshotTime.Value); await snapshot1Clone.FetchAttributesAsync(); AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties, false); CloudAppendBlob snapshotCopy = container.GetAppendBlobReference("blob2"); await snapshotCopy.StartCopyAsync((TestHelper.Defiddler(snapshot1.Uri))); await WaitForCopyAsync(snapshotCopy); Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status); using (Stream snapshotStream = (await snapshot1.OpenReadAsync())) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } await appendBlob.CreateOrReplaceAsync(); await appendBlob.FetchAttributesAsync(); // This is needed as cache settings are not updated with create call above. await blob.FetchAttributesAsync(); using (Stream snapshotStream = (await snapshot1.OpenReadAsync())) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } 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, (CloudBlob)blobs[0]); AssertAreEqual(snapshot2, (CloudBlob)blobs[1]); AssertAreEqual(appendBlob, (CloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (CloudBlob)blobs[3]); } finally { container.DeleteIfExistsAsync().Wait(); } }