Ash Robinson
Ash Robinson

Reputation: 61

Retrieving a blob filename

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();

    }
}

Upvotes: 3

Views: 16038

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136386

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("=======================================");
        }

Upvotes: 15

Related Questions