【问题标题】: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


    【解决方案1】:

    但我需要软删除文件而不是永久删除。

    因此,Storage SDK(和 REST API)中没有软删除 blob 的方法。只有删除 blob 操作。

    软删除是您在帐户级别设置的内容。正确配置软删除后,无论何时删除 blob(使用与您类似的代码),它将被软删除而不是永久删除,并且将根据设置在一段时间内保持软删除状态。

    请参阅此链接,了解如何为您的存储帐户配置 blob 软删除设置:https://docs.microsoft.com/en-us/azure/storage/blobs/soft-delete-blob-enable?tabs=azure-portal

    【讨论】:

      【解决方案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

      【讨论】:

        猜你喜欢
        • 2016-04-16
        • 2019-04-13
        • 1970-01-01
        • 2020-04-09
        • 2016-07-29
        • 2019-02-06
        • 2018-04-13
        • 2023-03-06
        • 2022-12-06
        相关资源
        最近更新 更多