Продажи: 1-800-867-1389

 none
Using Azure StorageClient, I want to Get list of "Directories" and "Files" in given container

Ответы

  • Try this code.  Basically, the thing to do is check the type on each of the IListBlobItems returned:

    var blobs = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudBlobClient();
    var container = blobs.GetContainerReference("testcontainer");
    container.CreateIfNotExist();
    container.GetBlobReference("directory/blob.txt").UploadText(string.Empty);
    container.GetBlobReference("blob.txt").UploadText(string.Empty);
    
    var items = container.ListBlobs();
    
    Console.WriteLine("Directories:");
    foreach (var dir in items.OfType<CloudBlobDirectory>())
    {
      Console.WriteLine("\t{0}", dir.Uri);
    }
    
    Console.WriteLine("Blobs:");
    foreach (var blob in items.OfType<CloudBlob>())
    {
      Console.WriteLine("\t{0}", blob.Uri);
    }
    
    

    • Помечено в качестве ответа Yi-Lun Luo 12 ноября 2010 г. 9:52
    6 ноября 2010 г. 19:59

Все ответы

  • If you're saying you want to get just the directory (and not the blobs at the same level), I don't think there's a way to do this.  (You can filter it client-side, but not on the server.)

    You can do the second half, though, by listing blobs with the prefix TopDir1.

    6 ноября 2010 г. 18:14
  • how would I filter it on the client side?  (that is where I actually want to filter it).  I know the azure blob explorers all do this because they manage to present me with view that shows the directory structure.  I'm must trying to do the same thing.

     

    cant find nice insert button for this so editing in html
    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    6 ноября 2010 г. 18:19
  • The following code appears to do something close to what you want. It gets a top-level directory and then iterates over the names in the subdirectory:

    protected void GetDirectoryList(String topLevelDirectoryName, String subDirectoryName)
    {
      CloudStorageAccount cloudStorageAccount =
        CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
      CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); 
    
      CloudBlobDirectory topLevelDirectory = cloudBlobClient.GetBlobDirectoryReferencetopLevelDirectoryName); 
    
      CloudBlobDirectory subDirectory = topLevelDirectory.GetSubdirectory(subDirectoryName); 
    
      IEnumerable<IListBlobItem> blobItems = subDirectory.ListBlobs();
      foreach (IListBlobItem blobItem in blobItems)
      {
        Uri uri = blobItem.Uri;
      }
    }
    
    
    
    
    
    
    
    
    
    • Предложено в качестве ответа MBytes 30 августа 2011 г. 11:58
    6 ноября 2010 г. 18:20
    Отвечающий
  • I'm still not getting it.  I tried the code above (with the changes I think I'm expected to make) and it does not give me a result (no blobItems).  I tried with no slash, front and back also.

    Also, I don't know the name of the subdirectory ("mydir1").  That is what I need to iterate for, even if the example did work, it does not give me a hint on how to get that name.

    private void buttonListContainer_Click(object sender, EventArgs e)
        {
          CloudBlobClient cloudBlobClient = _storageAccount.CreateCloudBlobClient();
          CloudBlobDirectory topLevelDirectory =
            cloudBlobClient.GetBlobDirectoryReference(
              "https://peterstest.blob.core.windows.net/3-6469723158585858?restype=container&comp=list");
          
          CloudBlobDirectory subDirectory = topLevelDirectory.GetSubdirectory("/mydir1");
    
          IEnumerable<IListBlobItem> blobItems = subDirectory.ListBlobs();
          foreach (IListBlobItem blobItem in blobItems)
          {
            Uri uri = blobItem.Uri;
          }

     


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    6 ноября 2010 г. 18:35
  • Note: I did take out the extra stuff after the url: ?restype=container&comp=list");

    and it did give me a list. 

    I still don't know how to get the top level (that is not "mydir1"), and how to tell if the files are directories.  That is, when I look in the debugger at the property of blobItem, it shows isFile = false whether or not it is the directory entry.


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    6 ноября 2010 г. 18:41
  • Continuing down this path, is the only way to tell if a blog is a "directory" is to see if it ends in a "/"?  Does that mean that blobnames that end in "/" are always directory entries, or could that confuse me on a file that was not really a directory.

    when I try to make a filename end in /, windows screams at me.

    screaming windows explorer

     

    Here is some modified code I'm messing around with to do this.

          CloudBlobClient cloudBlobClient = _storageAccount.CreateCloudBlobClient();
    
          CloudBlobDirectory topLevelDirectory =
            cloudBlobClient.GetBlobDirectoryReference(
              "https://peterstest.blob.core.windows.net/3-6469723158585858");
    
    
          IEnumerable<IListBlobItem> blobItemsTop = topLevelDirectory.ListBlobs();
          foreach (IListBlobItem blobItem in blobItemsTop)
          {
            Uri uri = blobItem.Uri;
          }
    
          CloudBlobDirectory subDirectory = topLevelDirectory.GetSubdirectory("mydir1");
          IEnumerable<IListBlobItem> blobItems = subDirectory.ListBlobs();
          foreach (IListBlobItem blobItem in blobItems)
          {
            Uri uri = blobItem.Uri;
          }
    

    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    6 ноября 2010 г. 18:54
  • IListBlobItem is the base class for CloudBlobDirectory, CloudBlockBlob, CloudBlockBlob and CloudPageBlob. You should be able to identify whether a particular item is a blob or a directory by just looking at its actual type.

    6 ноября 2010 г. 19:24
    Отвечающий
  • Try this code.  Basically, the thing to do is check the type on each of the IListBlobItems returned:

    var blobs = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudBlobClient();
    var container = blobs.GetContainerReference("testcontainer");
    container.CreateIfNotExist();
    container.GetBlobReference("directory/blob.txt").UploadText(string.Empty);
    container.GetBlobReference("blob.txt").UploadText(string.Empty);
    
    var items = container.ListBlobs();
    
    Console.WriteLine("Directories:");
    foreach (var dir in items.OfType<CloudBlobDirectory>())
    {
      Console.WriteLine("\t{0}", dir.Uri);
    }
    
    Console.WriteLine("Blobs:");
    foreach (var blob in items.OfType<CloudBlob>())
    {
      Console.WriteLine("\t{0}", blob.Uri);
    }
    
    

    • Помечено в качестве ответа Yi-Lun Luo 12 ноября 2010 г. 9:52
    6 ноября 2010 г. 19:59
  • Just so I have a good example (and understanding) for myself, I did a short blob post and included code for a simple windows form c# app that let's you view your blob storage as a directory tree.

    http://peterkellner.net/2010/11/12/azure-storage-treeviewer-directory-browser/

    post on treeviewer for blog
    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
    13 ноября 2010 г. 1:41