-
Notifications
You must be signed in to change notification settings - Fork 47
/
DeleteBlob.cs
91 lines (80 loc) · 3.17 KB
/
DeleteBlob.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
namespace BlobDevGuideBlobs
{
class DeleteBlob
{
// <Snippet_DeleteBlob>
public static async Task DeleteBlobAsync(BlobClient blob)
{
await blob.DeleteAsync();
}
// </Snippet_DeleteBlob>
// <Snippet_DeleteBlobSnapshots>
public static async Task DeleteBlobSnapshotsAsync(BlobClient blob)
{
// Delete a blob and all of its snapshots
await blob.DeleteAsync(snapshotsOption: DeleteSnapshotsOption.IncludeSnapshots);
// Delete only the blob's snapshots
//await blob.DeleteAsync(snapshotsOption: DeleteSnapshotsOption.OnlySnapshots);
}
// </Snippet_DeleteBlobSnapshots>
// <Snippet_RestoreBlob>
public static async Task RestoreBlobsAsync(BlobContainerClient container)
{
foreach (BlobItem blob in container.GetBlobs(BlobTraits.None, BlobStates.Deleted))
{
await container.GetBlockBlobClient(blob.Name).UndeleteAsync();
}
}
// </Snippet_RestoreBlob>
// <Snippet_RestoreSnapshot>
public static async Task RestoreSnapshotsAsync(
BlobContainerClient container,
BlobClient blob)
{
// Restore the deleted blob
await blob.UndeleteAsync();
// List blobs in this container that match prefix
// Include snapshots in listing
Pageable<BlobItem> blobItems = container.GetBlobs(
BlobTraits.None,
BlobStates.Snapshots,
prefix: blob.Name);
// Get the URI for the most recent snapshot
BlobUriBuilder blobSnapshotUri = new BlobUriBuilder(blob.Uri)
{
Snapshot = blobItems
.OrderByDescending(snapshot => snapshot.Snapshot)
.ElementAtOrDefault(0)?.Snapshot
};
// Restore the most recent snapshot by copying it to the blob
await blob.StartCopyFromUriAsync(blobSnapshotUri.ToUri());
}
// </Snippet_RestoreSnapshot>
// <Snippet_RestoreBlobWithVersioning>
public static void RestoreBlobWithVersioning(
BlobContainerClient container,
BlobClient blob)
{
// List blobs in this container that match prefix
// Include versions in listing
Pageable<BlobItem> blobItems = container.GetBlobs(
BlobTraits.None,
BlobStates.Version,
prefix: blob.Name);
// Get the URI for the most recent version
BlobUriBuilder blobVersionUri = new BlobUriBuilder(blob.Uri)
{
VersionId = blobItems.
OrderByDescending(version => version.VersionId).
ElementAtOrDefault(0)?.VersionId
};
// Restore the most recently generated version by copying it to the base blob
blob.StartCopyFromUri(blobVersionUri.ToUri());
}
// </Snippet_RestoreBlobWithVersioning>
}
}