Jan 24, 2012

How to Read all blob list from the Windows Azure Storage Containers

Read all Blob List Names from the Windows Azure Containers


The Following snippets is used to read all the Blob list details from the azure containers
and also we can avoid the transport connection error while reading the huge blob list.
by the paging segment concept can able to read large no of blob list...
       
 
              ///////// Getting Blobs List if it's below than 5000 ///////////  
 
             TextWriter  tw = new  StreamWriter ("d:/blob_thumblist.txt" );            
             int  pagingCount = 1000;
             CloudStorageAccount  storageAccount = CloudStorageAccount .Parse(ConfigurationSettings .AppSettings["CloudStorage" ].ToString());
             CloudBlobClient  blobClient = storageAccount.CreateCloudBlobClient();
             //Get a reference to the container. 
             CloudBlobContainer  container = blobClient.GetContainerReference("ContainerName" );
  
             ///////// Getting Blobs if's below than 5000 ///////////  
 
             //Return blobs using a flat listing. 
             BlobRequestOptions  options = new  BlobRequestOptions ();
             options.UseFlatBlobListing = true ;
 
             ////This first operation will return up to 5000 blobs. 
             ResultSegment <IListBlobItem > resultSegment = container.ListBlobsSegmented(options);
             foreach  (var  blobItem in  resultSegment.Results)
             { 
                 //writing the blob name into the text file  
                 tw.WriteLine(blobItem.Uri.Segments[2]);
             }
             //close the stream 
             tw.Close();
 
   
           ///////// Getting Blobs List if it's more  than 5000 /////////// 
 
             TextWriter  tw = new  StreamWriter ("d:/blob_thumblist.txt" ); 
             int  pagingCount = 1000;
             CloudStorageAccount  storageAccount = CloudStorageAccount .Parse(ConfigurationSettings .AppSettings["CloudStorage" ].ToString());
             CloudBlobClient  blobClient = storageAccount.CreateCloudBlobClient();
             //Get a reference to the container. 
             CloudBlobContainer  container = blobClient.GetContainerReference("ContainerName" );
             ///////// Getting Blobs if's more  than 5000 /////////// 
 
             ResultContinuation  continuationToken = resultSegment.ContinuationToken;
 
             //Check whether there are more results and list them in pages of 1000. 
             while  (continuationToken != null )
             {
                 resultSegment = container.ListBlobsSegmented(pagingCount, continuationToken, options);
                 foreach  (var  blobItem in  resultSegment.Results)
                 {
                     //writing the blob name into the text file  
                     Console .WriteLine(blobItem.Uri.Segments[2]);                           
                 }
         
                 //getting blob list count per Paging 
                 Console .WriteLine(resultSegment.Results.Count());
                 //Console.ReadLine(); 
                 continuationToken = resultSegment.ContinuationToken;
                 //calling next set of blob list 
                 resultSegment.GetNext();
        
             }
 
              //close the stream 
             tw.Close();
 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More