如何删除 Azure BLOB 存储中的所有文件
How to remove all files in Azure BLOB storage
背景:
我有一个系统,它与一个数据库一起工作,我在其中保存文件的元数据,而 Azure Blob 存储在我保存文件的地方。数据库和 Azure Blob Storage 通过网络服务协同工作。
为了检查系统的所有部分是否正常工作,我为下载、上传和删除文件的网络服务创建了单元测试。经过测试,数据库和 Azure Blob 存储保留了大量数据,我需要将它们全部删除。我有一个脚本可以从数据库中删除所有数据 (Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one sql statement)。
现在我需要编写一个 sctipt (power shell) 或代码 (C#) 来从 Azure Blob 存储中删除所有文件,但我不删除容器,只删除容器中的文件。
我的问题:
这些方法中哪种(power shell 或 С#)最好?
如果我使用 C# 和 tasks(System.Threading.Tasks) 来删除文件,它会更快吗?
最好的解决方法,如果你保存容器的标题,删除它们并尝试在几秒钟内重新创建它们(如果出现错误,你需要等待并重试),但如果你必须仅删除您可以使用的文件:
CloudStorageAccount storageAccount;
CloudBlobClient cloudBlobClient;
//connection is kept in app.config
storageAccount =
CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
cloudBlobClient = storageAccount.CreateCloudBlobClient();
Parallel.ForEach(cloudBlobClient.ListContainers(), x =>
{
Parallel.ForEach(x.ListBlobs(),y=>
{
((CloudBlockBlob)y).DeleteIfExists();
});
});
我不确定,但是,我登陆这里只是为了看看我如何一次性删除 blob 容器中的所有文件。从 azure 门户 UI,他们不提供任何功能 selected all for delete。
只需使用 Azure Storage Explorer,它具有 select 所有删除功能。我为我工作。
我知道这可能与这个问题的确切内容无关,但像我这样想手动删除的人会发现这很有帮助。
基于 Bushuev 回答。
2022 年更新
这是我完整的class 删除所有容器中的所有 blob(class 顶部列表中的容器除外)
唯一需要的参数是“连接字符串”
public class BlobStorageService : IBlobStorageService
{
private readonly List<string> _systemContainerNames = new List<string>()
{
"azure-webjobs-hosts"
};
public async Task CleanAllBlobsInAllContainers(string connectionString)
{
CloudBlobClient cloudBlobClient = CloudStorageAccount.Parse(connectionString)
.CreateCloudBlobClient();
ContainerResultSegment allContainers = await cloudBlobClient.ListContainersSegmentedAsync(default);
foreach (CloudBlobContainer container in allContainers.Results)
{
if (_systemContainerNames.Any(name => name.Equals(container.Name)))
continue;
BlobResultSegment allBlobs = await container.ListBlobsSegmentedAsync(default);
foreach (CloudBlockBlob blob in allBlobs.Results.OfType<CloudBlockBlob>())
{
await blob.DeleteIfExistsAsync();
}
}
}
}
背景: 我有一个系统,它与一个数据库一起工作,我在其中保存文件的元数据,而 Azure Blob 存储在我保存文件的地方。数据库和 Azure Blob Storage 通过网络服务协同工作。
为了检查系统的所有部分是否正常工作,我为下载、上传和删除文件的网络服务创建了单元测试。经过测试,数据库和 Azure Blob 存储保留了大量数据,我需要将它们全部删除。我有一个脚本可以从数据库中删除所有数据 (Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one sql statement)。
现在我需要编写一个 sctipt (power shell) 或代码 (C#) 来从 Azure Blob 存储中删除所有文件,但我不删除容器,只删除容器中的文件。
我的问题: 这些方法中哪种(power shell 或 С#)最好? 如果我使用 C# 和 tasks(System.Threading.Tasks) 来删除文件,它会更快吗?
最好的解决方法,如果你保存容器的标题,删除它们并尝试在几秒钟内重新创建它们(如果出现错误,你需要等待并重试),但如果你必须仅删除您可以使用的文件:
CloudStorageAccount storageAccount;
CloudBlobClient cloudBlobClient;
//connection is kept in app.config
storageAccount =
CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
cloudBlobClient = storageAccount.CreateCloudBlobClient();
Parallel.ForEach(cloudBlobClient.ListContainers(), x =>
{
Parallel.ForEach(x.ListBlobs(),y=>
{
((CloudBlockBlob)y).DeleteIfExists();
});
});
我不确定,但是,我登陆这里只是为了看看我如何一次性删除 blob 容器中的所有文件。从 azure 门户 UI,他们不提供任何功能 selected all for delete。
只需使用 Azure Storage Explorer,它具有 select 所有删除功能。我为我工作。
我知道这可能与这个问题的确切内容无关,但像我这样想手动删除的人会发现这很有帮助。
基于 Bushuev 回答。
2022 年更新
这是我完整的class 删除所有容器中的所有 blob(class 顶部列表中的容器除外)
唯一需要的参数是“连接字符串”
public class BlobStorageService : IBlobStorageService
{
private readonly List<string> _systemContainerNames = new List<string>()
{
"azure-webjobs-hosts"
};
public async Task CleanAllBlobsInAllContainers(string connectionString)
{
CloudBlobClient cloudBlobClient = CloudStorageAccount.Parse(connectionString)
.CreateCloudBlobClient();
ContainerResultSegment allContainers = await cloudBlobClient.ListContainersSegmentedAsync(default);
foreach (CloudBlobContainer container in allContainers.Results)
{
if (_systemContainerNames.Any(name => name.Equals(container.Name)))
continue;
BlobResultSegment allBlobs = await container.ListBlobsSegmentedAsync(default);
foreach (CloudBlockBlob blob in allBlobs.Results.OfType<CloudBlockBlob>())
{
await blob.DeleteIfExistsAsync();
}
}
}
}