Using Azure StorageClient, I want to Get list of "Directories" and "Files" in given container
-
06 Kasım 2010 Cumartesi 16:38
I've been looking through lots of articles (so please don't post links to them) regarding how to get a list of blob inside a given container. I want to be able to separate "directory" type blob's from non-directory. That is, if a blob container has 4 flies in it:
https://....blob.core.windows.net/3-6469723158585858/newa.txt
https://....blob.core.windows.net/3-6469723158585858/TopDir1
https://....blob.core.windows.net/3-6469723158585858/TopDir1/filea.txt
https://....blob.core.windows.net/3-6469723158585858/TopDir1/fileb.txt
I want to be able to first, get a list that just contains the one directory name (...TopDir1), then, using that name, get a list of the two files inside it (..TopDir1/filea.txt and ../TopDir1/fileb.txt
Thanks for any help on this.
Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider
Tüm Yanıtlar
-
06 Kasım 2010 Cumartesi 18:14
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.
-
06 Kasım 2010 Cumartesi 18:19
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.

Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider -
06 Kasım 2010 Cumartesi 18:20Yanıtlayıcı
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; } }
- Yanıt Olarak Öneren MBytes 30 Ağustos 2011 Salı 11:58
-
06 Kasım 2010 Cumartesi 18:35
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 -
06 Kasım 2010 Cumartesi 18:41
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 -
06 Kasım 2010 Cumartesi 18:54
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.
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 -
06 Kasım 2010 Cumartesi 19:24Yanıtlayıcı
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.
-
06 Kasım 2010 Cumartesi 19:59
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); }
- Yanıt Olarak İşaretleyen Yi-Lun LuoModerator 12 Kasım 2010 Cuma 09:52
-
13 Kasım 2010 Cumartesi 01:41
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/

Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider