Azure Blob存储,如何实现任何文档或文件的版本

标签 azure blob azure-blob-storage paas

在Azure Blob存储中,我想存储同一文件(文件名)的多个版本,我的意思是如何在Azure Blob存储中实现版本控制。

最佳答案

Azure Blob 版本控制现已全面推出。 从 Azure 门户,在“数据保护”>>“跟踪”>>“打开版本控制”选项下可用。 这将彻底解决您的问题。下面的链接提供了有关 Azure Blob 存储中文档版本控制的完整指南,其中包含 C# 代码示例。 https://subhankarsarkar.com/document-versioning-with-azure-blob-storage/

按版本下载文件的代码示例

public async Task<byte[]> DownloadBlobAsync(string fileToDownload, string fileVersion)
{
  using (var ms = new MemoryStream())
  {
    var blobClient = BlobContainerClient.GetBlockBlobClient(fileToDownload);
    // WithVersion() is the key piece here
    var blob = blobClient.WithVersion(fileVersion);
    await blob.DownloadToAsync(ms);
    return ms.ToArray();
  }
}

将文件恢复到特定版本

public void RestoreFileToSpecificVersion(string storageAccountName, string containerName, string fileName, string sourceVersion)
{
    var blobClient = BlobContainerClient.GetBlockBlobClient(fileName); // this is pointing to the current version
    //versionid={} is the most important piece here
    var sourceBlobUri = new Uri(
    string.Format("https://{0}.blob.core.windows.net/{1}/{2}?versionid={3}",
    storageAccountName, containerName, fileName, sourceVersion));

    // Since it will copy in the same storage account's container, it's a synchronous process
    // Copy Operation will make the specic version as current version
    // See https://learn.microsoft.com/en-us/rest/api/storageservices/copy-blob-from-url#request-headers
    blobClient.StartCopyFromUri(sourceBlobUri);
}

按版本删除文件

public async Task DeleteSpecificVersion(string fileName, string versionToDelete)
{
   var blobClient = BlobContainerClient.GetBlockBlobClient(fileName).WithVersion(versionToDelete);
   await blobClient.DeleteAsync();
}

关于Azure Blob存储,如何实现任何文档或文件的版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60836076/

相关文章:

android - 使用 fetch 在 react-native 中发布一个 blob

javascript - Blob 下载在 IE 中不起作用

azure - 修改Azure存储中的文本文件

c# - Azure 连接字符串 - 对象引用未设置为对象的实例

node.js - Azure Web App WebJob不安装 Node 包

azure - 在azure云服务中工作的多个实例

java - Web服务不接受 postman 的获取请求

c# - 如何在 Emgu 上为视频或相机捕捉进行白色 Blob 跟踪?

azure - 409 尝试将页面 Blob 恢复到以前的快照时发生冲突

flutter - Flutter 中的 Azure Active Directory 身份验证失败