3
votes

I am working on a Azure Storage project where i need to upload and download blobs in a container and list the container and blob in a listbox. I am unable to display the container and the blobs in my listbox.

This is my code to List: enter image description here

And finally the code behind the interface where i call my upload, download and list methods:

enter image description here

2
Any exceptions? Did you step through the code in the debugger? - Anthony Chu
we usually prefer if you can include the text of your code in the question, it makes reading - and testing - it easier. - Michael B

2 Answers

3
votes

The reason why you don't see any result when you click on Button3 in your webform is because you don't get back any data from the ListBlob method.

Change the ListBlob method to return a result like:

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

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

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

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

    // 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;
}

Than in your webform, I assume you have a ListBox with the name ListBox1. Call the method like:

protected void Button3_Click(object sender, EventArgs e)
{
    ListBox1.DataSource = GetBlobs();
    ListBox1.DataBind();
}
1
votes

It isn't clear to me what problem you are experiencing as you haven't explained fully. Listing blobs within a container including paging support is demonstrated in the following code extracted from this sample.

BlobContinuationToken token = null; 
do 
{ 
BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token); 
token = resultSegment.ContinuationToken; 
foreach (IListBlobItem blob in resultSegment.Results) 
{ 
// Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory 
Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType()); 
} 
} while (token != null);