1

i have create a Azure Cloud Service with WCF where i am trying to list my containers and blobs. I've been able to list both my containers and blobs. What i am trying to do is that when i select a container in my ListBox, it will display the blobs it contains in another ListBox.

Here's my code to list my containers:

    public List<string> ListContainer()
    {
        List<string> blobs = new List<string>();

        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("BlobConnectionString"));

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

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

        //Get the list of the blob from the above container

        IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers();

        foreach (CloudBlobContainer item in containers)
        {

            blobs.Add(string.Format("{0}", item.Uri));
        }

            return blobs;
    }

The code to list my blob:

        public List<string> ListBlob(string folder)
    {
        List<string> blobs = new List<string>();

        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("BlobConnectionString"));

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

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


        //Get the list of the blob from the above container
        IEnumerable<IListBlobItem> blobList = container.ListBlobs();

        //Display the names on the page
        string names = string.Empty;

        foreach (IListBlobItem item in blobList)
        {
            blobs.Add(string.Format("Directory {0}", item.Uri));
        }

In my web form, i call my ListContainer methods in the page load:

 protected void Page_Load(object sender, EventArgs e)
    {
        BlobService list = new BlobService();
        ListBox2.DataSource = list.ListContainer();
        ListBox2.DataBind();

    }

When i load the project, my containers are listed in the ListBox 2. What i need is that when i click on a container, it will display the blobs it contains in another ListBox. I tried the following but nothing happens:

protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ListBox2.SelectedItem.Equals("http://127.0.0.1:10000/devstorageaccount1/mycontainer"))
        {
            BlobService list = new BlobService();
            ListBox1.DataSource = list.ListBlob("mycontainer");
            ListBox1.DataBind();
        }

        else
        {
            //Error Message
        }
Huby03
  • 801
  • 2
  • 14
  • 28

3 Answers3

1

The event handler ListBox2_SelectedIndexChanged will be called during a postback to the server if the index has changed. In order to start the postback whenever the user selects a new item, you need to set AutoPostBack="True" on ListBox2 in the aspx-markup (see this link for details):

<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True" 
    OnSelectedIndexChanged="ListBox2_SelectedIndexChanged" />
Markus
  • 20,838
  • 4
  • 31
  • 55
  • Thank you for your quick answer, when i click the "mycontainer" item in my listbox, i got an error with my link: http://127.0.0.1:10000/devstorageaccount1/mycontainer NullReferenceException was unhandled by user code. An exception of type 'System.NullRefenceException' occured in WCF REST Service.dll but was not handled in the user code – Huby03 Feb 13 '16 at 14:32
  • @Huby03: the following post and its answers describe the NullReferenceException and how to fix it in great detail: http://stackoverflow.com/q/4660142/642579. I hope this helps. – Markus Feb 14 '16 at 13:28
0

Also you might need to add the DataTextField to specify what is shown and DataValueField to specify the key that you want to use.

xmorera
  • 1,933
  • 3
  • 20
  • 35
0

I cant see your ListBlob(string folder) method full code, it has a method to get the blob in this article https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/ you could change your ListBlob(string folder) method to get a result:

public List<string> ListBlob(string folder)
        {
            List<string> blobs = new List<string>();

            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("BlobConnectionString"));

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

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

            // Loop over items within the container and output the length and URI.
            foreach (IListBlobItem item in container.ListBlobs(null, false))
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;

                    blobs.Add(string.Format("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri));

                }
                else if (item.GetType() == typeof(CloudPageBlob))
                {
                    CloudPageBlob pageBlob = (CloudPageBlob)item;

                    blobs.Add(string.Format("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri));

                }
                else if (item.GetType() == typeof(CloudBlobDirectory))
                {
                    CloudBlobDirectory directory = (CloudBlobDirectory)item;

                    blobs.Add(string.Format("Directory: {0}", directory.Uri)); ;
                }
            }
            return blobs;
        }


protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {           
                BlobService list = new BlobService();
                ListBox1.DataSource = list.ListBlob("mycontainer");
                ListBox1.DataBind();

        }

Keep contact if you have any questions.

Lily_user4045
  • 795
  • 4
  • 11
  • I am working on the Local development storage, that's why i'm using "http://127.0.0.1:10000/devstorageaccount1/mycontainer". I'm able to list both of my list and containers but i want to be able to click on a specific container and all its blob will be displayed in another listbox, still unable to do it, i really really stuck! – Huby03 Feb 17 '16 at 05:53
  • Do you mean it's ok when deploy to Azure storage but cant get result when deploy in local emulator? – Lily_user4045 Feb 18 '16 at 07:24
  • I'm not deploying to Azure storage, i just need to upload it in my local storage! But it is not working and my Listbox items don't have any index that i can call or it has but i don't know how to do it.. – Huby03 Feb 18 '16 at 07:44
  • do you install storage emulator? – Lily_user4045 Feb 18 '16 at 08:59
  • Yes, i have already uploaded some file in the local storage, i just need to display them when i click on a specific container in my ListBox! – Huby03 Feb 18 '16 at 09:05
  • I have a test, it has no problem. You said you have an error NullReferenceException. Pls make sure you create local container. You could new container in your code or download azurestorageexplorer from http://azurestorageexplorer.codeplex.com/, create local container in azurestorageexplorer. You will get data. Keep contact if you have any questions. – Lily_user4045 Feb 19 '16 at 10:02