在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/