Microsoft Azure – Storage – Part 3 – List the contents of the container with a .NET application

In the second part of the Azure Storage series we have developed a .Net application that can create a Microsoft Azure storage  container and upload files into it. Today we will extend the functionality of our application to list the files in the container.

List the files in the container

First let’s create a class that represents the Azure Blob. This way we can isolate our console application from the Azure Storage system, so in the future it can work with any cloud storage systems.

Add a file to the Azure_Helper project to store enumerations

  • Right click the Azure_Helper project and select Add -> Class
  • Name the class Enums
  • Add an enumeration outside of the class definition. The file should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nsAzure_Helper {

    public class Enums {
    }

    public enum ItemType {
        undefined
        , BlobDirectory
        , BlockBlob
        , PageBlob

    }
}

Add a class to the Azure_Helper to represent a file or directory in the cloud

  • Right click the Azure_Helper project and select Add -> Class
  • Name the class CloudItem
  • To make the class public enter the word public in front of the word class
public class CloudItem
  • Add public properties to the class. The class definition should look like this:
public class CloudItem {

    public long Length { get; set; }
    public Uri Uri { get; set; }
    public ItemType Itemtype { get; set; }

}
  • Open the Azure_Helper.cs file in the Azure_Helper project
  • Add a new method to list the content of the container
/// <summary>
/// Returns the list of cloud items in the container
/// </summary>
/// <param name="sContainerPath"></param>
/// <returns></returns>
public List<CloudItem> GetContainerItems(string sContainerPath) {

    List<CloudItem> itemList = null;

    // Create the blob client. 
    CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

    if (null == container) {
        // No container found
        return itemList;
    }

    // Create an instance of the item list
    itemList = new List<CloudItem>();

    // Loop over items within the container and output the length and URI.
    foreach (IListBlobItem item in container.ListBlobs(null, false)) {

        // Create a new instance of the Cloud Item
        CloudItem cloudItem = new CloudItem();

        if (item.GetType() == typeof(CloudBlockBlob)) {
            // This is a Block Blob

            CloudBlockBlob blob = (CloudBlockBlob)item;

            cloudItem.Itemtype = ItemType.BlockBlob;
            cloudItem.Length = blob.Properties.Length;
            cloudItem.Uri = blob.Uri;

        } else if (item.GetType() == typeof(CloudPageBlob)) {
            // This is a Page Blob

            CloudPageBlob pageBlob = (CloudPageBlob)item;

            cloudItem.Itemtype = ItemType.PageBlob;
            cloudItem.Length = pageBlob.Properties.Length;
            cloudItem.Uri = pageBlob.Uri;

        } else if (item.GetType() == typeof(CloudBlobDirectory)) {
            // This is a Directory

            CloudBlobDirectory directory = (CloudBlobDirectory)item;

            cloudItem.Itemtype = ItemType.BlobDirectory;
            cloudItem.Uri = directory.Uri;

        }

        // Add the item to the list
        itemList.Add(cloudItem);

    }

    return itemList;

}

The method above returns a list of Cloud Items.

Let’s call the method from our Console Application

  • Open the Program.cs file in the Azure_ConsoleApplication project
  • Add the following code to the existing lines
// Get the list of items in the Container
List<CloudItem> itemList = azureHelper.GetContainerItems(sContainerPath);
if (null != itemList){

    foreach (CloudItem item in itemList){

        if (ItemType.BlockBlob == item.Itemtype) {

            Console.WriteLine("Block blob of length {0}: {1}", item.Length, item.Uri);

        } else if (ItemType.PageBlob == item.Itemtype) {

            Console.WriteLine("Page blob of length {0}: {1}", item.Length, item.Uri);

        } else if (ItemType.BlobDirectory == item.Itemtype) {

            Console.WriteLine("Directory: {0}", item.Uri);
        }

    }
}

Console.WriteLine("Press a key to exit");
Console.Read();

The Program.cs file should look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using nsAzure_Helper;

namespace Azure_ConsoleApplication {
	class Program {
		static void Main(string[] args) {

			// Create an instance of the Azure Helper
			Azure_Helper azureHelper = new Azure_Helper();

			string sContainerPath = "test1";
			bool bMakeContainerPublic = true;
			string sBlobName = "blob1";
			string sSourceFilePath =  @"D:\MediaFiles\Audio\Music.wav";

			// Create a public container
			azureHelper.CreateContainer(sContainerPath, bMakeContainerPublic);

			// Upload an audio file
			azureHelper.UploadFile(sContainerPath, sBlobName, sSourceFilePath);

			// Get the list of items in the Container
			List itemList = azureHelper.GetContainerItems(sContainerPath);
			if (null != itemList){

				foreach (CloudItem item in itemList){

					if (ItemType.BlockBlob == item.Itemtype) {

						Console.WriteLine("Block blob of length {0}: {1}", item.Length, item.Uri);

					} else if (ItemType.PageBlob == item.Itemtype) {

						Console.WriteLine("Page blob of length {0}: {1}", item.Length, item.Uri);

					} else if (ItemType.BlobDirectory == item.Itemtype) {

						Console.WriteLine("Directory: {0}", item.Uri);
					}

				}
			}

			Console.WriteLine("Press a key to exit");
			Console.Read();

		}

	}
}

In the next article we will learn how to download files from the Microsoft Azure storage.

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *