在 .NET 中创建和管理 blob 快照Create and manage a blob snapshot in .NET

快照是在某一时间点拍摄的只读版本的 Blob。A snapshot is a read-only version of a blob that's taken at a point in time. 本文介绍如何使用适用于 .NET 的 Azure 存储客户端库创建和管理 blob 快照。This article shows how to create and manage blob snapshots using the Azure Storage client library for .NET.

有关 Azure 存储中 blob 快照的详细信息,请参阅 Blob 快照For more information about blob snapshots in Azure Storage, see Blob snapshots.

创建快照Create a snapshot

要使用 12.x 版用于 .NET 的 Azure 存储客户端库创建块 blob 快照,请使用以下任一方法: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:

以下代码示例演示如何在版本 12.x 中创建快照。The following code example shows how to create a snapshot with version 12.x. 包含对 Azure.Identity 库的引用,以使用 Azure AD 凭据授权对服务的请求。Include a reference to the Azure.Identity library to use your Azure AD credentials to authorize requests to the service.

private static async Task CreateBlockBlobSnapshot(string accountName, string containerName, string blobName, Stream data)
{
    const string blobServiceEndpointSuffix = ".blob.core.chinacloudapi.cn";
    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

若要删除 blob,必须先删除该 blob 的所有快照。To delete a blob, you must first delete any snapshots of that blob. 可以单独删除快照,或指定在删除源 Blob 时删除所有快照。You can delete a snapshot individually, or specify that all snapshots be deleted when the source blob is deleted. 如果尝试删除仍包含快照的 Blob,会发生错误。If you attempt to delete a blob that still has snapshots, an error results.

要使用 12.x 版用于 .NET 的 Azure 存储客户端库删除 blob 及其快照,请使用以下任一方法,并包括 DeleteSnapshotsOption 枚举: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 enum:

以下代码示例演示如何在 .NET 中删除 blob 及其快照,其中 blobClientBlobClient 类型的对象: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);

后续步骤Next steps