Skip to content

Latest commit

 

History

History
128 lines (91 loc) · 6.09 KB

File metadata and controls

128 lines (91 loc) · 6.09 KB
title Create and manage a blob snapshot with .NET
titleSuffix Azure Storage
description Learn how to use the .NET client library to create a read-only snapshot of a blob to back up blob data at a given moment in time.
author stevenmatthew
ms.author shaas
ms.service azure-blob-storage
ms.topic how-to
ms.date 08/05/2024
ms.devlang csharp
ms.custom devx-track-csharp, devguide-csharp, devx-track-dotnet

Create and manage a blob snapshot with .NET

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.

For more information about blob snapshots in Azure Storage, see Blob snapshots.

[!INCLUDE storage-dev-guide-prereqs-dotnet]

Set up your environment

[!INCLUDE storage-dev-guide-project-setup-dotnet]

Authorization

The authorization mechanism must have the necessary permissions to work with blob snapshots. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for Snapshot Blob.

Create a snapshot

To create a snapshot of a block blob, use one of the following methods:

The following code example shows how to create a snapshot. Include a reference to the Azure.Identity library to use your Microsoft Entra credentials to authorize requests to the service. For more information about using the DefaultAzureCredential class to authorize a managed identity to access Azure Storage, see Azure Identity client library for .NET.

private static async Task CreateBlockBlobSnapshot(
    string accountName,
    string containerName, 
    string blobName,
    Stream data)
{
    const string blobServiceEndpointSuffix = ".blob.core.windows.net";
    Uri containerUri = 
        new Uri("https://" + accountName + blobServiceEndpointSuffix + "/" + containerName);

    // Get a container client object and create the container.
    BlobContainerClient containerClient = new BlobContainerClient(containerUri,
        new DefaultAzureCredential());
    await containerClient.CreateIfNotExistsAsync();

    // Get a blob client object.
    BlobClient blobClient = containerClient.GetBlobClient(blobName);

    try
    {
        // Upload text to create a block blob.
        await blobClient.UploadAsync(data);

        // Add blob metadata.
        IDictionary<string, string> metadata = new Dictionary<string, string>
        {
            { "ApproxBlobCreatedDate", DateTime.UtcNow.ToString() },
            { "FileType", "text" }
        };
        await blobClient.SetMetadataAsync(metadata);

        // Sleep 5 seconds.
        System.Threading.Thread.Sleep(5000);

        // Create a snapshot of the base blob.
        // You can specify metadata at the time that the snapshot is created.
        // If no metadata is specified, then the blob's metadata is copied to the snapshot.
        await blobClient.CreateSnapshotAsync();
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}

Delete snapshots

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.

To delete a blob and its snapshots, use one of the following methods, and include the DeleteSnapshotsOption enum:

The following code example shows how to delete a blob and its snapshots in .NET, where blobClient is an object of type BlobClient:

await blobClient.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, default);

Copy a blob snapshot over the base blob

You can perform a copy operation to promote a snapshot over its base blob, as long as the base blob is in an online tier (hot or cool). The snapshot remains, but its destination is overwritten with a copy that can be read and written to.

The following code example shows how to copy a blob snapshot over the base blob:

:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/CopySnapshot.cs" id="Snippet_CopySnapshot":::

Resources

To learn more about managing blob snapshots using the Azure Blob Storage client library for .NET, see the following resources.

For related code samples using deprecated .NET version 11.x SDKs, see Code samples using .NET version 11.x.

[!INCLUDE storage-dev-guide-resources-dotnet]

See also