【问题标题】:Soft delete blob File in Azure blob container in Dot.Net CoreDot.Net Core 中 Azure blob 容器中的软删除 blob 文件
【发布时间】:2021-10-09 04:56:22
【问题描述】:
我想从我的 DotNet Core 应用程序中软删除我的 azure blob 容器中的 blob 文件。我正在使用“Azure.Storage.Blobs”nuget 包。我设法从 azure blob 容器中永久删除 blob 文件,但我需要软删除文件而不是永久删除。我浏览了 Microsoft 文档 (https://azure.microsoft.com/en-us/blog/soft-delete-for-azure-storage-blobs-ga/) 以使用代码来完成它,但我没有得到很多。
我已经给出了用于删除 blob 文件的代码。请建议我如何从代码中软删除文件。
{
BlobContainerClient container = new
BlobContainerClient(blobConnectionString, containerName);
//Delete Blob
var response=container.DeleteBlob(blobNamewithFileExtension);
}
【问题讨论】:
标签:
azure
.net-core
azure-table-storage
azure-blob-storage
soft-delete
【解决方案2】:
正如@gaurav-mantri 所述,首先应该在 Blob 存储帐户级别的策略/设置上允许它,然后只能通过代码完成。它也不是软删除,它包括创建版本、相同 blob 的快照并恢复它们或删除它们。所以需要看Documentation以获得完整的详细了解。
如果尚未通过如下配置完成,您必须首先为 blob 启用软删除:
// Retrieve a CloudBlobClient object and enable soft delete
CloudBlobClient blobClient = GetCloudBlobClient();
try
{
ServiceProperties serviceProperties = blobClient.GetServiceProperties();
serviceProperties.DeleteRetentionPolicy.Enabled = true;
serviceProperties.DeleteRetentionPolicy.RetentionDays = RetentionDays;
blobClient.SetServiceProperties(serviceProperties);
}
catch (StorageException ex)
{
Console.WriteLine("Error returned from the service: {0}", ex.Message);
throw;
}
用于软删除:
CloudBlockBlob blockBlob = container.GetBlockBlobReference("HelloWorld");
//Create or update blob
await blockBlob.UploadFromFileAsync(ImageToUpload);
// Snapshot
await blockBlob.SnapshotAsync();
// Delete (including snapshots)
blockBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
// Undelete
await blockBlob.UndeleteAsync();
// Recover
Console.WriteLine("\nCopy the most recent snapshot over the base blob:");
IEnumerable<IListBlobItem> allBlobVersions = container.ListBlobs(
prefix: blockBlob.Name, useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Snapshots);
CloudBlockBlob copySource = allBlobVersions.First(version => ((CloudBlockBlob)version).IsSnapshot &&
((CloudBlockBlob)version).Name == blockBlob.Name) as CloudBlockBlob;
blockBlob.StartCopy(copySource);
更多详情请参考:
https://docs.microsoft.com/en-us/azure/storage/blobs/soft-delete-blob-overview
https://github.com/Azure-Samples/storage-dotnet-blob-soft-delete