List directories in Windows Azure Blob storage container

醉酒当歌 提交于 2020-01-01 01:52:50

问题


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.

Who can help me with this "problem"?


回答1:


Try this code. It makes use of 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);
        }

If you're using Storage Client Library 1.8 (i.e. previous to version 2.0), try this code:

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



回答2:


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

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

and

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

in the same foreach to list every layer of virtual folders, BlockBlobs and PageBlobs differently. Hope this helps.



来源:https://stackoverflow.com/questions/14503772/list-directories-in-windows-azure-blob-storage-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!