public async Task CloudBlobContainerListBlobsAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); List <string> blobNames = await CreateBlobsAsync(container, 3, BlobType.PageBlob); BlobResultSegment results = await container.ListBlobsSegmentedAsync(null); Assert.AreEqual(blobNames.Count, results.Results.Count()); foreach (IListBlobItem blobItem in results.Results) { Assert.IsInstanceOfType(blobItem, typeof(CloudPageBlob)); Assert.IsTrue(blobNames.Remove(((CloudPageBlob)blobItem).Name)); } } finally { container.DeleteIfExistsAsync().Wait(); } }
public async Task CloudBlobDirectoryFlatListingAsync() { foreach (String delimiter in Delimiters) { CloudBlobClient client = GenerateCloudBlobClient(); client.DefaultDelimiter = delimiter; string name = GetRandomContainerName(); CloudBlobContainer container = client.GetContainerReference(name); try { await container.CreateAsync(); if (await CloudBlobDirectorySetupWithDelimiterAsync(container, delimiter)) { BlobResultSegment segment = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter, false, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList1 = new List <IListBlobItem>(); simpleList1.AddRange(segment.Results); while (segment.ContinuationToken != null) { segment = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter, false, BlobListingDetails.None, null, segment.ContinuationToken, null, null); simpleList1.AddRange(segment.Results); } Assert.IsTrue(simpleList1.Count == 3); IListBlobItem item11 = simpleList1.ElementAt(0); Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1")); IListBlobItem item12 = simpleList1.ElementAt(1); Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter)); IListBlobItem item13 = simpleList1.ElementAt(2); Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter)); CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13; BlobResultSegment segment2 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList2 = new List <IListBlobItem>(); simpleList2.AddRange(segment2.Results); while (segment2.ContinuationToken != null) { segment2 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, segment2.ContinuationToken, null, null); simpleList2.AddRange(segment2.Results); } Assert.IsTrue(simpleList2.Count == 2); IListBlobItem item21 = simpleList2.ElementAt(0); Assert.IsTrue(item21.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter + "EndBlob1")); IListBlobItem item22 = simpleList2.ElementAt(1); Assert.IsTrue(item22.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter + "EndBlob2")); BlobResultSegment segment3 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList3 = new List <IListBlobItem>(); simpleList3.AddRange(segment3.Results); while (segment3.ContinuationToken != null) { segment3 = await container.ListBlobsSegmentedAsync("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, segment3.ContinuationToken, null, null); simpleList3.AddRange(segment3.Results); } Assert.IsTrue(simpleList3.Count == 2); IListBlobItem item31 = simpleList3.ElementAt(0); Assert.IsTrue(item31.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter)); IListBlobItem item32 = simpleList3.ElementAt(1); Assert.IsTrue(item32.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter)); BlobResultSegment segment4 = await midDir2.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, null, null, null); List <IListBlobItem> simpleList4 = new List <IListBlobItem>(); simpleList4.AddRange(segment4.Results); while (segment4.ContinuationToken != null) { segment4 = await midDir2.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, segment4.ContinuationToken, null, null); simpleList4.AddRange(segment4.Results); } Assert.IsTrue(simpleList4.Count == 2); IListBlobItem item41 = simpleList4.ElementAt(0); Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1")); IListBlobItem item42 = simpleList4.ElementAt(1); Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2")); } } finally { container.DeleteIfExistsAsync().Wait(); } } }
public async Task CloudBlobSnapshotAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudBlockBlob blockBlob = container.GetBlockBlobReference(BlobName); await blockBlob.UploadFromStreamAsync(originalData); 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); // The query parser should not be case sensitive in detecting a snapshot in the query string CloudBlob snapshotParseVerifier = new CloudBlob(new Uri(blob.Uri + "?sNapshOt=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials); Assert.IsNotNull(snapshotParseVerifier.SnapshotTime, "Snapshot parse verifier did not successfully detect the snapshot time"); Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshotParseVerifier.SnapshotTime.Value); CloudBlob snapshotCopy = container.GetBlobReference("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 blockBlob.PutBlockListAsync(new List <string>()); 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(blob, (CloudBlob)blobs[2]); AssertAreEqual(snapshotCopy, (CloudBlob)blobs[3]); } finally { await container.DeleteAsync();; } }
public async Task CloudBlobSoftDeleteSnapshotTask() { CloudBlobContainer container = GetRandomContainerReference(); try { //Enables a delete retention policy on the blob with 1 day of default retention days container.ServiceClient.EnableSoftDelete(); await container.CreateAsync(); // Upload some data to the blob. MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); await appendBlob.UploadFromStreamAsync(originalData); CloudBlob blob = container.GetBlobReference(BlobName); //create snapshot via api CloudBlob snapshot = await blob.SnapshotAsync(); //create snapshot via write protection await appendBlob.UploadFromStreamAsync(originalData); //we should have 2 snapshots 1 regular and 1 deleted: there is no way to get only the deleted snapshots but the below listing will get both snapshot types int blobCount = 0; int deletedSnapshotCount = 0; int snapShotCount = 0; BlobContinuationToken ct = null; do { foreach (IListBlobItem item in (await container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.Snapshots | BlobListingDetails.Deleted, null, ct, null, null)) .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); if (blobItem.IsSnapshot) { snapShotCount++; } if (blobItem.IsDeleted) { Assert.IsNotNull(blobItem.Properties.DeletedTime); Assert.AreEqual(blobItem.Properties.RemainingDaysBeforePermanentDelete, 0); deletedSnapshotCount++; } blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 3); Assert.AreEqual(deletedSnapshotCount, 1); Assert.AreEqual(snapShotCount, 2); blobCount = 0; ct = null; do { foreach (IListBlobItem item in (await container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.Snapshots, null, ct, null, null)) .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 2); //Delete Blob and snapshots await blob.DeleteAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null); Assert.IsFalse(await blob.ExistsAsync()); Assert.IsFalse(await snapshot.ExistsAsync()); blobCount = 0; ct = null; do { foreach (IListBlobItem item in (await container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null)) .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsTrue(blobItem.IsDeleted); Assert.IsNotNull(blobItem.Properties.DeletedTime); Assert.AreEqual(blobItem.Properties.RemainingDaysBeforePermanentDelete, 0); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 3); await blob.UndeleteAsync(); await blob.FetchAttributesAsync(); Assert.IsFalse(blob.IsDeleted); Assert.IsNull(blob.Properties.DeletedTime); Assert.IsNull(blob.Properties.RemainingDaysBeforePermanentDelete); blobCount = 0; ct = null; do { foreach (IListBlobItem item in (await container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null)) .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 3); } finally { container.ServiceClient.DisableSoftDelete(); await container.DeleteAsync(); } }
public async Task CloudBlobSoftDeleteNoSnapshotTask() { CloudBlobContainer container = GetRandomContainerReference(); try { //Enables a delete retention policy on the blob with 1 day of default retention days container.ServiceClient.EnableSoftDelete(); await container.CreateAsync(); // Upload some data to the blob. MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024)); CloudAppendBlob appendBlob = container.GetAppendBlobReference(BlobName); await appendBlob.CreateOrReplaceAsync(); await appendBlob.AppendBlockAsync(originalData, null); CloudBlob blob = container.GetBlobReference(BlobName); Assert.IsTrue(await blob.ExistsAsync()); Assert.IsFalse(blob.IsDeleted); await blob.DeleteAsync(); Assert.IsFalse(await blob.ExistsAsync()); int blobCount = 0; BlobContinuationToken ct = null; do { foreach (IListBlobItem item in (await container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null)) .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsTrue(blobItem.IsDeleted); Assert.IsNotNull(blobItem.Properties.DeletedTime); Assert.AreEqual(blobItem.Properties.RemainingDaysBeforePermanentDelete, 0); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 1); await blob.UndeleteAsync(); await blob.FetchAttributesAsync(); Assert.IsFalse(blob.IsDeleted); Assert.IsNull(blob.Properties.DeletedTime); Assert.IsNull(blob.Properties.RemainingDaysBeforePermanentDelete); blobCount = 0; ct = null; do { foreach (IListBlobItem item in (await container.ListBlobsSegmentedAsync (null, true, BlobListingDetails.All, null, ct, null, null)) .Results .ToList()) { CloudAppendBlob blobItem = (CloudAppendBlob)item; Assert.AreEqual(blobItem.Name, BlobName); Assert.IsFalse(blobItem.IsDeleted); Assert.IsNull(blobItem.Properties.DeletedTime); Assert.IsNull(blobItem.Properties.RemainingDaysBeforePermanentDelete); blobCount++; } } while (ct != null); Assert.AreEqual(blobCount, 1); } finally { container.ServiceClient.DisableSoftDelete(); await container.DeleteAsync(); } }
public async Task CloudBlobSnapshotTask() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); var blobName = GetRandomBlobName(); 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(); 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); CloudBlob snapshotCopy = container.GetBlobReference("blob2"); await snapshotCopy.StartCopyAsync(snapshot1.Uri, null, null, null, null); 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); } //overwriting will create another snapshot await appendBlob.CreateOrReplaceAsync(); await blob.FetchAttributesAsync(); using (Stream snapshotStream = await snapshot1.OpenReadAsync()) { snapshotStream.Seek(0, SeekOrigin.End); TestHelper.AssertStreamsAreEqual(originalData, snapshotStream); } await Task.Delay(500); List <IListBlobItem> blobs = (await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null)) .Results .ToList(); Assert.AreEqual(4, blobs.Count); AssertAreEqual(snapshotCopy, (CloudBlob)blobs[0]); AssertAreEqual(snapshot1, (CloudBlob)blobs[1]); AssertAreEqual(snapshot2, (CloudBlob)blobs[2]); AssertAreEqual(blob, (CloudBlob)blobs[3]); } finally { await container.DeleteIfExistsAsync(); } }