16

我对我的项目有疑问...我需要知道如何列出 Windows Azure blob 存储中的所有文件夹(在字符串列表或其他内容中)...我已经拥有 BlobClient 和与 Azure 存储的连接。

谁能帮我解决这个“问题”?

4

2 回答 2

23

试试这个代码。它利用Storage Client library 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);
        }

如果您使用的是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);
        }
于 2013-01-24T15:09:38.290 回答
4

我在我的项目中使用了这个解决方案

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

在我的情况下,我在 listView 中显示结果,列出大小和日期,使用

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

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

在同一个foreach中以不同的方式列出每一层的虚拟文件夹,BlockBlobs和PageBlobs。希望这可以帮助。

于 2013-07-24T08:52:08.537 回答