简体   繁体   English

列出Windows Azure Blob存储容器中的目录

[英]List directories in Windows Azure Blob storage container

I have a question about my project... I need to know how to list all folders (in a string list or something) from a Windows Azure blob storage... I allready have my BlobClient and the connection to my Azure storage. 我对我的项目有疑问......我需要知道如何从Windows Azure blob存储中列出所有文件夹(在字符串列表或其他内容中)...我已经拥有了我的BlobClient以及与Azure存储的连接。

Who can help me with this "problem"? 谁可以帮我解决这个“问题”?

Try this code. 试试这个代码。 It makes use of Storage Client library 2.0.3 : 它使用Storage Client库2.0.3

        CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
        CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("wad-control-container");
        string blobPrefix = null;
        bool useFlatBlobListing = false;
        var blobs = blobContainer.ListBlobs(blobPrefix, useFlatBlobListing, BlobListingDetails.None);
        var folders = blobs.Where(b => b as CloudBlobDirectory != null).ToList();
        foreach (var folder in folders)
        {
            Console.WriteLine(folder.Uri);
        }

If you're using Storage Client Library 1.8 (ie previous to version 2.0) , try this code: 如果您使用的是Storage Client Library 1.8(即2.0之前的版本) ,请尝试以下代码:

        var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
        cloudBlobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = cloudBlobClient.GetContainerReference("wad-control-container");
        IEnumerable<IListBlobItem> blobs = container.ListBlobs(new BlobRequestOptions()
        {
            UseFlatBlobListing = false,
        });
        var folders = blobs.Where(b => b as CloudBlobDirectory != null);

        foreach (var folder in folders)
        {
            Console.WriteLine(folder.Uri);
        }

I've used this solution in my project 我在我的项目中使用过这个解决方案

// Retrieve reference to the container.
CloudBlobContainer container = BlobClient.GetContainerReference(lvContainer.SelectedItems[0].Text);

//Loop over VIRTUAL directories within the container
foreach (IListBlobItem item in container.ListBlobs())
{
      if (item.GetType() == typeof(CloudBlobDirectory))
      {
           CloudBlobDirectory directory = (CloudBlobDirectory)item;
           string[] uri = directory.Uri.ToString().Split('/');
           ListViewItem dir = new ListViewItem();
           dir.Text = uri[uri.Length-2];
           dir.ImageIndex = 0;

           ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem();
           subitem.Text = String.Empty; //item.Properties.LastModifiedUtc.ToString();
           dir.SubItems.Add(subitem);

           subitem = new ListViewItem.ListViewSubItem();
           subitem.Text = String.Empty; //item.Properties.Length + " bytes";
           dir.SubItems.Add(subitem);

           lvBlob.Items.Add(dir);
       }
}

In my case I was displaying the results in a listView, listing size and date, using 在我的情况下,我在listView中显示结果,列出大小和日期,使用

(item.GetType() == typeof(CloudBlockBlob))

and

(item.GetType() == typeof(CloudPageBlob))

in the same foreach to list every layer of virtual folders, BlockBlobs and PageBlobs differently. 在同一个foreach中列出每个虚拟文件夹层,BlockBlobs和PageBlobs不同。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM