在Windows Azure的Blob存储容器列出目录(List directories in Wi

2019-08-08 03:59发布

我有一个关于我的项目的问题...我需要知道如何列出文件夹(在一个字符串列表或某物)从一个Windows Azure的Blob存储......我媒体链接有我BlobClient和我的Azure存储的连接。

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

Answer 1:

试试这个代码。 它利用存储客户端库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);
        }

如果您使用的存储客户端库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);
        }


Answer 2:

我用在我的项目该解决方案

// 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不同的每一层。 希望这可以帮助。



文章来源: List directories in Windows Azure Blob storage container