3

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
  • 2
    Any exceptions? Did you step through the code in the debugger? Commented Feb 10, 2016 at 17:16
  • 3
    we usually prefer if you can include the text of your code in the question, it makes reading - and testing - it easier. Commented Feb 10, 2016 at 17:55

2 Answers 2

3

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you :) but now when i call the method in my webform i have a problem with the GetBlob(); The name GetBlob() does not exist in the current context!
Well it works perfectly now, i forgot to create an object of my class! I did as follow: BlobService list = new BlobService(); ListBox1.DataSource = list.ListBlob(); ListBox1.DataBind(); Thanks a lot for helping me! Regards
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.