以编程方式批量删除 Azure blob 存储对象

Programmatically delete Azure blob storage objects in bulks

是否可以通过编程方式批量删除 Azure blob 对象? 一个一个地删除对象有时需要几天的时间。

我们在 Azure Blob 存储上放置了很多新文件,我们希望删除所有过时的文件以避免不必要的费用。

我在网络/MSDN/Stack Overflow 上进行了搜索,发现 2014 年在 MSDN 上只有一个主题是指在 Microsoft 网站上创建功能请求。

Is it possible to programmatically delete Azure blob objects in bulks?

没有。您需要一个接一个地删除 blob。

要加快该过程,您可以做的是创建不同的容器(比如为每个 year/month 组合创建一个容器)并将相关的 blob 放入其中。当您需要删除那个 month/year 的 blob 时,您只需删除那个容器。

当然可以(即使用 Powershell)

$Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
Get-AzureStorageBlob -Container "container-name" -Context $Ctx -Blob TellMeWhatToDelete* | Remove-AzureStorageBlob

即:这将删除文档容器中的每个 Powerpoint 演示文稿

Get-AzureStorageBlob -Container "documents" -Context $Ctx -Blob *.pptx | Remove-AzureStorageBlob

或者您可以删除过去 30 天内未更改的所有 docx 文件

Get-AzureStorageBlob -Container "documents" -Context $Ctx -Blob *.docx
| where {$_.LastModified -le (get-date).AddDays(-30) } | Remove-AzureStorageBlob

你可以批量删除blob。那将是一对一的通话。

您只需要:

  1. Blob 的 URI 列表。
  2. 连接到您的存储帐户
  3. 从包管理器添加使用 Azure.Storage.Blobs

在 return 中,如果删除 blob 有任何问题,异常将为您提供详细信息。

  BlobServiceClient service = new BlobServiceClient(connectionStringForStorageAccount);

            BlobBatchClient batchClient = service.GetBlobBatchClient();
            try
            {
                  //dont forget to add the include snapshots :)
               await batchClient.DeleteBlobsAsync(listofURIforBlobs,
    Azure.Storage.Blobs.Models.DeleteSnapshotsOption.IncludeSnapshots);

            }
            catch (Exception ex)
            {
                return (false, ex.Message);
            }