Blob 版本控制会在修改或删除 Blob 时自动创建以前版本的 Blob。 启用 blob 版本控制后,如果错误地修改或删除了数据,则可以还原 blob 的先前版本以恢复数据。
为了获得最佳数据保护,Azure 建议为存储帐户启用 Blob 版本控制和 Blob 软删除。 有关详细信息,请参阅 Blob 版本控制和 Blob 软删除。
修改 blob 以触发新版本
下面的代码示例演示如何使用适用于 .NET 的 Azure 存储客户端库(版本 12.5.1 或更高版本)触发新版本的创建。 运行此示例之前,请确保已为存储帐户启用版本控制。
该示例将创建一个块 blob,然后更新该 blob 的元数据。 更新 blob 的元数据会触发新版本的创建。 该示例将检索初始版本和当前版本,并显示只有当前版本包含元数据。
public static async Task UpdateVersionedBlobMetadata(BlobContainerClient blobContainerClient, 
                                                     string blobName)
{
    try
    {
        // Create the container.
        await blobContainerClient.CreateIfNotExistsAsync();
        // Upload a block blob.
        BlockBlobClient blockBlobClient = blobContainerClient.GetBlockBlobClient(blobName);
        string blobContents = string.Format("Block blob created at {0}.", DateTime.Now);
        byte[] byteArray = Encoding.ASCII.GetBytes(blobContents);
        string initalVersionId;
        using (MemoryStream stream = new MemoryStream(byteArray))
        {
            Response<BlobContentInfo> uploadResponse = 
                await blockBlobClient.UploadAsync(stream, null, default);
            // Get the version ID for the current version.
            initalVersionId = uploadResponse.Value.VersionId;
        }
        // Update the blob's metadata to trigger the creation of a new version.
        Dictionary<string, string> metadata = new Dictionary<string, string>
        {
            { "key", "value" },
            { "key1", "value1" }
        };
        Response<BlobInfo> metadataResponse = 
            await blockBlobClient.SetMetadataAsync(metadata);
        // Get the version ID for the new current version.
        string newVersionId = metadataResponse.Value.VersionId;
        // Request metadata on the previous version.
        BlockBlobClient initalVersionBlob = blockBlobClient.WithVersion(initalVersionId);
        Response<BlobProperties> propertiesResponse = await initalVersionBlob.GetPropertiesAsync();
        PrintMetadata(propertiesResponse);
        // Request metadata on the current version.
        BlockBlobClient newVersionBlob = blockBlobClient.WithVersion(newVersionId);
        Response<BlobProperties> newPropertiesResponse = await newVersionBlob.GetPropertiesAsync();
        PrintMetadata(newPropertiesResponse);
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}
static void PrintMetadata(Response<BlobProperties> propertiesResponse)
{
    if (propertiesResponse.Value.Metadata.Count > 0)
    {
        Console.WriteLine("Metadata values for version {0}:", propertiesResponse.Value.VersionId);
        foreach (var item in propertiesResponse.Value.Metadata)
        {
            Console.WriteLine("Key:{0}  Value:{1}", item.Key, item.Value);
        }
    }
    else
    {
        Console.WriteLine("Version {0} has no metadata.", propertiesResponse.Value.VersionId);
    }
}
列出 blob 版本
若要列出 blob 版本,请在“版本”字段中指定 BlobStates 参数。 版本将按最旧到最新的顺序列出。
下面的代码示例演示如何列出 blob 版本。
private static void ListBlobVersions(BlobContainerClient blobContainerClient, 
                                           string blobName)
{
    try
    {
        // Call the listing operation, specifying that blob versions are returned.
        // Use the blob name as the prefix. 
        var blobVersions = blobContainerClient.GetBlobs
            (BlobTraits.None, BlobStates.Version, prefix: blobName)
            .OrderByDescending(version => version.VersionId).Where(blob => blob.Name == blobName);
        // Construct the URI for each blob version.
        foreach (var version in blobVersions)
        {
            BlobUriBuilder blobUriBuilder = new BlobUriBuilder(blobContainerClient.Uri)
            {
                BlobName = version.Name,
                VersionId = version.VersionId
            };
            if ((bool)version.IsLatestVersion.GetValueOrDefault())
            {
                Console.WriteLine("Current version: {0}", blobUriBuilder);
            }
            else
            {
                Console.WriteLine("Previous version: {0}", blobUriBuilder);
            }
        }
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}
通过基本 Blob 复制以前的 Blob 版本
可以执行复制操作,以通过其基本 Blob 提升版本,前提是基本 blob 位于联机层(热或冷)。 快照将会保留,但会通过可以读取或写入的副本覆盖其目标。
下面的代码示例演示了如何在基本 Blob 上复制 Blob 版本:
public static async Task<BlockBlobClient> CopyVersionOverBaseBlobAsync(
    BlockBlobClient client,
    string versionTimestamp)
{
    // Instantiate BlobClient with identical URI and add version timestamp
    BlockBlobClient versionClient = client.WithVersion(versionTimestamp);
    // Restore the specified version by copying it over the base blob
    await client.SyncUploadFromUriAsync(versionClient.Uri);
    // Return the client object after the copy operation
    return client;
}
资源
要详细了解如何使用适用于 .NET 的 Azure Blob 存储客户端库来管理 blob 版本,请参阅以下资源。