且构网

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

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

更新时间:2023-02-14 21:06:19

Windows Azure Blob存储不具有文件夹的概念.层次结构非常简单:存储帐户>容器> blob .实际上,删除特定文件夹就是删除所有以文件夹名称开头的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").