且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除 Azure blob 容器中的文件夹

更新时间:2023-02-14 20:38:08

Windows Azure Blob Storage 没有文件夹的概念.层次结构非常简单:存储帐户>容器 >斑点.事实上,删除特定文件夹就是删除所有以文件夹名称开头的 blob.您可以编写如下简单的代码来删除您的文件夹:

Windows Azure Blob Storage does not have the concept of folders. The hierarchy is very simple: storage account > container > blob. In fact, removing a particular folder is removing all the blobs which start with the folder name. You can write the simple code as below to delete your folders:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("your storage account");
CloudBlobContainer container = storageAccount.CreateCloudBlobClient().GetContainerReference("pictures");
foreach (IListBlobItem blob in container.GetDirectoryReference("users").ListBlobs(true))
{
    if (blob.GetType() == typeof(CloudBlob) || blob.GetType().BaseType == typeof(CloudBlob))
    {
        ((CloudBlob)blob).DeleteIfExists();
    }
}

container.GetDirectoryReference("users").ListBlobs(true) 列出以users"开头的 blob;在图片"内容器,然后您可以单独删除它们.要删除其他文件夹,您只需要像这样指定GetDirectoryReference(您的文件夹名称").

container.GetDirectoryReference("users").ListBlobs(true) lists the blobs start with "users" within the "picture" container, you can then delete them individually. To delete other folders, you just need to specify like this GetDirectoryReference("your folder name").