Retrieving a blob filename

asp.net, azure, azure-blob-storage, c#

Solution

Try this code. Code assumes that all blobs in your blob container are of type block blobs.

Storage Client Library 2.0:

        CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
        CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("images");
        var blobs = blobContainer.ListBlobs(null, true, BlobListingDetails.All).Cast<CloudBlockBlob>();
        foreach (var blockBlob in blobs)
        {
            Console.WriteLine("Name: " + blockBlob.Name);
            Console.WriteLine("Size: " + blockBlob.Properties.Length);
            Console.WriteLine("Content type: " + blockBlob.Properties.ContentType);
            Console.WriteLine("Download location: " + blockBlob.Uri);
            Console.WriteLine("=======================================");
        }

Storage Client Library 1.7:

        CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
        CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("images");
        var blobs = blobContainer.ListBlobs(new BlobRequestOptions()
            {
                BlobListingDetails = BlobListingDetails.All,
                UseFlatBlobListing = true,
            }).Cast<CloudBlockBlob>();
        foreach (var blockBlob in blobs)
        {
            Console.WriteLine("Name: " + blockBlob.Name);
            Console.WriteLine("Size: " + blockBlob.Properties.Length);
            Console.WriteLine("Content type: " + blockBlob.Properties.ContentType);
            Console.WriteLine("Download location: " + blockBlob.Uri);
            Console.WriteLine("=======================================");
        }

Problem

So I'm trying to retrieve details of a file I have in blob storage. The idea being that clients ask for documents to be placed on their portal that relate specifically to them. This is a migration and currently the files are listed in a grid in the format: File Name, File Size, File Type, Download Link. The bit I'm having trouble with is retrieving the blob properties. Here is a code snippet of what I have currently. ``` public void BindGridDocuments() { var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString); var blobStorage = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobStorage.GetContainerReference("documents"); var documentCollection = container.ListBlobs(); foreach (var document in documentCollection) { string filename = document.Uri.ToString(); } } ```

Original source