locked
How do I list blobs in a specific folder? RRS feed

  • Question

  • I have

    temp/images/a.jpg
    temp/images/b.jpg

    Could you show the code to list all the blobs in folder "temp/images"?  Thanks a lot.

    Friday, April 1, 2011 4:34 PM

Answers

All replies

  • Hi Ray

    please have a look at this msdn entry. It contains a sample.

    • Proposed as answer by dropoutcoder Friday, April 1, 2011 6:54 PM
    Friday, April 1, 2011 4:52 PM
  • Hi Marc,

    Thank you for the link.  I reviewed and ran the code.  It lists all the blobs in the container that I specified. 

    But what I am looking for is that given a container, say "mycontainer" and in this contain there are folders says "images" and in the "images" folder there are image blobs there.

    So how do you just get the references of the blobs in that "images" folder?

    Ray.

    Friday, April 1, 2011 11:18 PM
  • I think I got it,

    var dirRef = container.GetDirectoryReference("images");
    var items = dirRef.ListBlobs();
    
    

    The code above will list the blobs just in the "images" folder.

    A new question actually comes up, how do you delete an IListBlobItem obj? Ihave the following where the items is the items I got from the above code.

    foreach (var b in items)       { 
              CloudBlob blob = blobClient.GetBlobReference(b.Uri.AbsolutePath); 
              blob.Delete();         
    }       
    }

    this code throw exception on the blob.Delete(), the message says "The requested URI does not represent any resource on the server." Any ideas?

    Saturday, April 2, 2011 12:02 AM
  • Hi,

    Based on the documentation for Uri.AbsolutePath (http://msdn.microsoft.com/en-us/library/system.uri.absolutepath.aspx), it only returns the part of the URI and not the complete URI and thus you're getting 404 error (resource not found). Try changing b.Uri.AbsolutePath to b.Url.AbsoluteUri and see if that helps.

    Hope this helps.

    Thanks

    Gaurav Mantri

    Cerebrata Software

    http://www.cerebrata.com

     

    • Marked as answer by ray247ray Sunday, April 3, 2011 10:03 PM
    Sunday, April 3, 2011 4:42 AM
  • @Ray247, A IListBlobItem is either a CloudBlob or a CloudBlobDirectory - you could check to see what it is and use the concrete class to perform the delete.

    Thanks,

    jai

    Sunday, April 3, 2011 8:20 PM