user8711824
user8711824

Reputation: 11

List items from blob container C# .net

Need Help,

Trying to get a list for items stored in blob-container however unable to understand why it times-out.

The error that I get is: -

ReadTimeout '((System.IO.Stream)(swa.BaseStream)).ReadTimeout' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}

Detail error message:

enter image description here

Used the code from: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-dotnet-how-to-use-blobs

so as on below code - resultSegment gets Result count = 2327 but on listblob.txt only exports - 2279 then it timesout

namespace listblob
{
    class Program
    {

         static void Main(string[] args)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("StorageConnectionString"));

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("containername");

            ListBlobsSegmentedInFlatListing(container).Wait();
        }

        async public static Task ListBlobsSegmentedInFlatListing(CloudBlobContainer container)
        {
            int i = 0;
            BlobContinuationToken continuationToken = null;
            BlobResultSegment resultSegment = null;
            string patha = @"C:\listblob.txt";
            StreamWriter swa = File.CreateText(patha);

            do
            {
                resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);
                if (resultSegment.Results.Count<IListBlobItem>() > 0) //{ Console.WriteLine("Page {0}:", ++i); }
                foreach (var blobItem in resultSegment.Results)
                {
                    //Console.WriteLine("\t{0}", blobItem.StorageUri.PrimaryUri);
                    swa.WriteLine(blobItem.Uri.Segments[2]);
                }
.
                continuationToken = resultSegment.ContinuationToken;
            }
            while (continuationToken != null);
        }

    }

}

Upvotes: 0

Views: 2096

Answers (2)

Rohan
Rohan

Reputation: 39

List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();

try using this way

Upvotes: 0

user8711824
user8711824

Reputation: 11

Thanks for the reply Thennarasan. i tried as per you suggestion but had no change on result.

However i Copied all to String List and got the expected result.

List<String> list = new List<String>();
list.Add(blobItem.Uri.Segments[2]);
System.IO.File.WriteAllLines("SavedLists.txt", list);

Upvotes: 1

Related Questions