Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
1.60/5 (3 votes)
Hello
I write this code to search the blob items (text files) on the basis of there content. For ex : if I search for "Good", then the files that contains "Good or good" word the name of that files should appear in search result. My code is working but i want to optimize it.

C#
class BlobSearch
    {
        public static int num = 1;
        static void Main(string[] args)
        {
            string accountName = "accountName";
            string accessKey = "accesskey";
            string azureConString = "DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + accessKey;
			string blob = "MyBlobContainer";
            string searchText = string.Empty;

            Console.WriteLine("Type and enter to search : ");
            searchText = Console.ReadLine();

            CloudStorageAccount account = CloudStorageAccount.Parse(azureConString);
            CloudBlobClient blobClient = account.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(blob);

            blobContainer.FetchAttributes();
            var blobItemList = blobContainer.ListBlobs();
            GetBlobList(searchText, blobContainer, blobItemList);

            Console.ReadLine();
        }

              private static async void GetBlobList(string searchText, CloudBlobContainer blobContainer, IEnumerable<IListBlobItem> blobItemList)
        {
            foreach (var item in blobItemList)
            {               
                CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(item.Uri.ToString());
                if (blockBlob.Name.Contains(".txt"))
                {
                    await Search(searchText, blockBlob);
                }
            }
        }


        private async static Task Search(string searchText, CloudBlockBlob blockBlob)
        {
            string text = await blockBlob.DownloadTextAsync();
            if (text.ToLower().IndexOf(searchText.ToLower()) != -1)
            {
                Console.WriteLine("Result : " + num + " => " + blockBlob.Name.Substring(blockBlob.Name.LastIndexOf('/') + 1));
                num++;
            }
        }
    }


I think
C#
blobContainer.ListBlobs();
is blocking code because search result not work until all the blob items loaded. Is there anyway to optimize it or anywhere else in my code.

Thanks
Posted
Updated 10-Jun-14 2:42am
v5

1 solution

if its possible you can go on with spinning up multiple thread , to process the blob items as async process !

u can use the following code , it will for sure improve the performance :

async public static Task ListBlobsSegmentedInFlatListing()
{
    // 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("myblobs");

    //List blobs in pages.
    Console.WriteLine("List blobs in pages:");

    //List blobs with a paging size of 10, for the purposes of the example. 
    //The first call does not include the continuation token.
    BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(
            "", true, BlobListingDetails.All, 10, null, null, null);

    //Enumerate the result segment returned.
    int i = 0;
    if (resultSegment.Results.Count<IListBlobItem>() > 0) { Console.WriteLine("Page {0}:", ++i); }
    foreach (var blobItem in resultSegment.Results)
    {
        Console.WriteLine("\t{0}", blobItem.StorageUri.PrimaryUri);
    }
    Console.WriteLine();

    //Get the continuation token, if there are additional pages of results.
    BlobContinuationToken continuationToken = resultSegment.ContinuationToken;

    //Check whether there are more results and list them in pages of 10 while a continuation token is returned.
    while (continuationToken != null)
    {
        //This overload allows control of the page size. 
        //You can return all remaining results by passing null for the maxResults parameter, 
        //or by calling a different overload.
        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);
        }
        Console.WriteLine();

        //Get the next continuation token.
        continuationToken = resultSegment.ContinuationToken;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900