List directories in Windows Azure Blob storage container

前端 未结 2 1049
灰色年华
灰色年华 2021-02-07 10:35

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 a

2条回答
  •  独厮守ぢ
    2021-02-07 11:20

    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.

提交回复
热议问题