|
| 1 | +--- |
| 2 | +title: Create and manage a blob snapshot in .NET |
| 3 | +titleSuffix: Azure Storage |
| 4 | +description: Learn how to create a read-only snapshot of a blob to back up blob data at a given moment in time. |
| 5 | +services: storage |
| 6 | +author: tamram |
| 7 | + |
| 8 | +ms.service: storage |
| 9 | +ms.topic: article |
| 10 | +ms.date: 04/02/2020 |
| 11 | +ms.author: tamram |
| 12 | +ms.subservice: blobs |
| 13 | +--- |
| 14 | + |
| 15 | +# Create and manage a blob snapshot in .NET |
| 16 | + |
| 17 | +A snapshot is a read-only version of a blob that's taken at a point in time. This article shows how to create and manage blob snapshots using the [Azure Storage client library for .NET](/dotnet/api/overview/azure/storage?view=azure-dotnet). |
| 18 | + |
| 19 | +For more information about blob snapshots in Azure Storage, see [Create and manage a blob snapshot in .NET](snapshots-overview.md). |
| 20 | + |
| 21 | +## Create a snapshot |
| 22 | + |
| 23 | +# [.NET version 12.x](#tab/v12) |
| 24 | + |
| 25 | +To create a snapshot of a block blob using version 12.x of the Azure Storage client library for .NET, use one of the following methods: |
| 26 | + |
| 27 | +- [CreateSnapshot](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.createsnapshot) |
| 28 | +- [CreateSnapshotAsync](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.createsnapshotasync) |
| 29 | + |
| 30 | +The following code example shows how to create a snapshot with version 12.x. Include a reference to the [Azure.Identity](https://www.nuget.org/packages/azure.identity) library to use your Azure AD credentials to authorize requests to the service. |
| 31 | + |
| 32 | +```csharp |
| 33 | +private static async Task CreateBlockBlobSnapshot(string accountName, string containerName, string blobName, Stream data) |
| 34 | +{ |
| 35 | + const string blobServiceEndpointSuffix = ".blob.core.windows.net"; |
| 36 | + Uri containerUri = new Uri("https://" + accountName + blobServiceEndpointSuffix + "/" + containerName); |
| 37 | + |
| 38 | + // Get a container client object and create the container. |
| 39 | + BlobContainerClient containerClient = new BlobContainerClient(containerUri, |
| 40 | + new DefaultAzureCredential()); |
| 41 | + await containerClient.CreateIfNotExistsAsync(); |
| 42 | + |
| 43 | + // Get a blob client object. |
| 44 | + BlobClient blobClient = containerClient.GetBlobClient(blobName); |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + // Upload text to create a block blob. |
| 49 | + await blobClient.UploadAsync(data); |
| 50 | + |
| 51 | + // Add blob metadata. |
| 52 | + IDictionary<string, string> metadata = new Dictionary<string, string> |
| 53 | + { |
| 54 | + { "ApproxBlobCreatedDate", DateTime.UtcNow.ToString() }, |
| 55 | + { "FileType", "text" } |
| 56 | + }; |
| 57 | + await blobClient.SetMetadataAsync(metadata); |
| 58 | + |
| 59 | + // Sleep 5 seconds. |
| 60 | + System.Threading.Thread.Sleep(5000); |
| 61 | + |
| 62 | + // Create a snapshot of the base blob. |
| 63 | + // You can specify metadata at the time that the snapshot is created. |
| 64 | + // If no metadata is specified, then the blob's metadata is copied to the snapshot. |
| 65 | + await blobClient.CreateSnapshotAsync(); |
| 66 | + } |
| 67 | + catch (RequestFailedException e) |
| 68 | + { |
| 69 | + Console.WriteLine(e.Message); |
| 70 | + Console.ReadLine(); |
| 71 | + throw; |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +# [.NET version 11.x](#tab/v11) |
| 77 | + |
| 78 | +To create a snapshot of a block blob using version 11.x of the Azure Storage client library for .NET, use one of the following methods: |
| 79 | + |
| 80 | +- [CreateSnapshot](/dotnet/api/microsoft.azure.storage.blob.cloudblockblob.createsnapshot) |
| 81 | +- [CreateSnapshotAsync](/dotnet/api/microsoft.azure.storage.blob.cloudblockblob.createsnapshotasync) |
| 82 | + |
| 83 | +The following code example shows how to create a snapshot with version 11.x. This example specifies additional metadata for the snapshot when it is created. |
| 84 | + |
| 85 | +```csharp |
| 86 | +private static async Task CreateBlockBlobSnapshot(CloudBlobContainer container) |
| 87 | +{ |
| 88 | + // Create a new block blob in the container. |
| 89 | + CloudBlockBlob baseBlob = container.GetBlockBlobReference("sample-base-blob.txt"); |
| 90 | + |
| 91 | + // Add blob metadata. |
| 92 | + baseBlob.Metadata.Add("ApproxBlobCreatedDate", DateTime.UtcNow.ToString()); |
| 93 | + |
| 94 | + try |
| 95 | + { |
| 96 | + // Upload the blob to create it, with its metadata. |
| 97 | + await baseBlob.UploadTextAsync(string.Format("Base blob: {0}", baseBlob.Uri.ToString())); |
| 98 | + |
| 99 | + // Sleep 5 seconds. |
| 100 | + System.Threading.Thread.Sleep(5000); |
| 101 | + |
| 102 | + // Create a snapshot of the base blob. |
| 103 | + // You can specify metadata at the time that the snapshot is created. |
| 104 | + // If no metadata is specified, then the blob's metadata is copied to the snapshot. |
| 105 | + Dictionary<string, string> metadata = new Dictionary<string, string>(); |
| 106 | + metadata.Add("ApproxSnapshotCreatedDate", DateTime.UtcNow.ToString()); |
| 107 | + await baseBlob.CreateSnapshotAsync(metadata, null, null, null); |
| 108 | + Console.WriteLine(snapshot.SnapshotQualifiedStorageUri.PrimaryUri); |
| 109 | + } |
| 110 | + catch (StorageException e) |
| 111 | + { |
| 112 | + Console.WriteLine(e.Message); |
| 113 | + Console.ReadLine(); |
| 114 | + throw; |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## Delete snapshots |
| 122 | + |
| 123 | +To delete a blob, you must first delete any snapshots of that blob. You can delete a snapshot individually, or specify that all snapshots be deleted when the source blob is deleted. If you attempt to delete a blob that still has snapshots, an error results. |
| 124 | + |
| 125 | +# [.NET version 12.x](#tab/v12) |
| 126 | + |
| 127 | +To delete a blob and its snapshots using version 12.x of the Azure Storage client library for .NET, use one of the following methods, and include the [DeleteSnapshotsOption](/dotnet/api/azure.storage.blobs.models.deletesnapshotsoption) enum: |
| 128 | + |
| 129 | +- [Delete](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.delete) |
| 130 | +- [DeleteAsync](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.deleteasync) |
| 131 | +- [DeleteIfExists](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.deleteifexists) |
| 132 | +- [DeleteIfExistsAsync](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.deleteifexistsasync) |
| 133 | + |
| 134 | +The following code example shows how to delete a blob and its snapshots in .NET, where `blobClient` is an object of type [BlobClient](/dotnet/api/azure.storage.blobs.blobclient)): |
| 135 | + |
| 136 | +```csharp |
| 137 | +await blobClient.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, default); |
| 138 | +``` |
| 139 | + |
| 140 | +# [.NET version 11.x](#tab/v11) |
| 141 | + |
| 142 | +To delete a blob and its snapshots using version 11.x of the Azure Storage client library for .NET, use one of the following blob deletion methods, and include the [DeleteSnapshotsOption](/dotnet/api/microsoft.azure.storage.blob.deletesnapshotsoption) enum: |
| 143 | + |
| 144 | +- [Delete](/dotnet/api/microsoft.azure.storage.blob.cloudblob.delete) |
| 145 | +- [DeleteAsync](/dotnet/api/microsoft.azure.storage.blob.cloudblob.deleteasync) |
| 146 | +- [DeleteIfExists](/dotnet/api/microsoft.azure.storage.blob.cloudblob.deleteifexists) |
| 147 | +- [DeleteIfExistsAsync](/dotnet/api/microsoft.azure.storage.blob.cloudblob.deleteifexistsasync) |
| 148 | + |
| 149 | +The following code example shows how to delete a blob and its snapshots in .NET, where `blockBlob` is an object of type [CloudBlockBlob][dotnet_CloudBlockBlob]: |
| 150 | + |
| 151 | +```csharp |
| 152 | +await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null); |
| 153 | +``` |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## Next steps |
| 158 | + |
| 159 | +- [Blob snapshots](snapshots-overview.md) |
| 160 | +- [Blob versions (preview)](versioning-overview.md) |
| 161 | +- [Soft delete for blobs](storage-blob-soft-delete.md) |
0 commit comments