我正在尝试删除 Blob 存储中树下名为“缓存”的容器中的所有内容。
我的结构是这样的
-Root
-Bin
-Media
-1324
-cached
-5648
-cached
-Images
-cached
我想删除“缓存”文件夹中“媒体”下的所有内容。
有什么好的方法呢?手写代码?我有大约 100,000 个文件夹,其中包含我想删除的“缓存”名称。
我正在尝试删除 Blob 存储中树下名为“缓存”的容器中的所有内容。
我的结构是这样的
-Root
-Bin
-Media
-1324
-cached
-5648
-cached
-Images
-cached
我想删除“缓存”文件夹中“媒体”下的所有内容。
有什么好的方法呢?手写代码?我有大约 100,000 个文件夹,其中包含我想删除的“缓存”名称。
也许一些正则表达式可以解决问题?
string pattern = @"/devstoreaccount1/Root/Media/([A-Za-z0-9\-]+)/cached/([A-Za-z0-9\-]+)";
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
foreach (var blob in blobClient.GetContainerReference("Root").ListBlobs(new BlobRequestOptions { UseFlatBlobListing = true }))
{
if (Regex.Match(blob.Uri.AbsolutePath, pattern).Success)
{
((CloudBlockBlob)blob).Delete();
}
}
当然,您应该首先针对存储模拟器中的一些测试数据进行测试,并注意当您切换到真正的云存储时,需要调整该模式。
希望能帮助到你...
这是使用 Azure Storage 4.3.0.0 的新方法
public void DeleteFolder(string Container, string Prefix)
{
if (!string.IsNullOrEmpty(Prefix))
{
var _Container = GetBlobContainer(Container);
var _Blobs = _Container.ListBlobs(Prefix, true);
foreach (IListBlobItem blob in _Blobs)
{
_Container.GetBlockBlobReference(((CloudBlockBlob)blob).Name).DeleteIfExists();
}
}
}
public CloudBlobContainer GetBlobContainer(string container)
{
// Retrieve storage account from connection string.
CloudStorageAccount _StorageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient _BlobClient = _StorageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer _Container = _BlobClient.GetContainerReference(container);
// Retrieve reference to a blob named "myblob".
return _Container;
}
在 Windows Azure 存储中,您只有 1 个容器深度。其他所有内容实际上都是 blob 名称的一部分。因此,在您的情况下,您有一个“根”容器和一堆名为“media/1324/cached/blobname”的 blob 文件。在这种情况下,它只是一个带有分隔符的长字符串'/'。
在您的场景中,使用“媒体”的 ListBlobs 操作的“前缀”过滤器枚举“根”容器下的每个 Blob 是最简单的。一旦您将 blob 过滤为以“媒体”开头,然后遍历它们并找到其中也包含“缓存”的那些。
如果您选择了不同的命名约定,您可以让 Blob 存储与您一起查找文件。但是,您需要切换名称,以便首先出现“缓存”(例如“媒体/缓存/1234/blobname”)。然后,您可以再次使用 ListBlobs 按前缀进行过滤,并且只返回以“媒体/缓存”开头的 Blob。
您可以随时使用http://azurestorageexplorer.codeplex.com/ 无需编写任何代码