使用blob存储中的名称删除所有包含

时间:2012-04-13 21:52:30

标签: azure azure-storage azure-storage-blobs

我正在尝试删除blob存储中树下名为“cached”的容器中的所有内容。

我的结构是这样的

-Root
    -Bin
    -Media
       -1324
         -cached
       -5648
         -cached
    -Images
       -cached

我想删除“缓存”文件夹中“媒体”下的所有内容。

对此有什么好处?手工编码?我有大约100,000个具有“缓存”名称的文件夹,我想删除它。

4 个答案:

答案 0 :(得分:3)

也许一些正则表达式可以解决这个问题?

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();
    }
}

当然,您应首先针对存储模拟器中的某些测试数据进行测试,并注意在切换到真实云存储时需要调整模式。

希望它有所帮助...

答案 1 :(得分:3)

以下是使用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;
        }

答案 2 :(得分:1)

在Windows Azure存储中,您只有1个容器深度。其他一切实际上都是blob名称的一部分。所以,在你的情况下,你有一个'root'容器和一堆名为'media / 1324 / cached / blobname'的blob文件。它只是一个带有分隔符的长字符串,在这种情况下是'/'。

在您的场景中,最简单的方法是使用“media”的ListBlobs操作的“prefix”过滤器枚举“root”容器下的每个blob。将blob过滤后以'media'开头,然后迭代它们并找到其中也有'cache'的那些。

如果您选择了不同的命名约定,那么您可以使用blob存储来查找文件。您需要切换名称,以便首先显示“缓存”(例如“media / cache / 1234 / blobname”)。然后,您可以再次使用ListBlobs按前缀过滤,并仅返回以“media / cache”开头的blob。

答案 3 :(得分:-3)

您始终可以使用http://azurestorageexplorer.codeplex.com/ 无需编写任何代码

相关问题