Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I am done with writing the code to upload files (text files) to azure blob storage. Now I want to provide search based on text files content. For ex. If I search for "Hello" then the name of files that contains "Hello" words should appear in search result. Here my code to search
C#
class BlobSearch
{
    static void Main(string[] args)
    {
        string searchText = "Hello";
        CloudStorageAccount account = CloudStorageAccount.Parse(azureConString);
        CloudBlobClient blobClient = account.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference("MyBlobContainer");

        blobContainer.FetchAttributes();

        var blobItemList = blobContainer.ListBlobs();

        foreach (var item in blobItemList)
        {
            string line = string.Empty;
            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(item.Uri.ToString());

            if(blockBlob.Name.Contains(".txt"))
            {
                int lineno = 1;
                using (var stream = blockBlob.OpenRead())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        while ((line = reader.ReadLine()) != null)
                        {
                            if (line.IndexOf(searchText) != -1)
                            {
                                Console.WriteLine("Line : " + lineno  +" => "+ blockBlob.Name);
                            }
                            lineno++;
                        }
                    }
                }
            }
        }
        Console.WriteLine("SEARCH COMPLETE");
        Console.ReadLine();
    }
}


Above code is working but it is too slow. Is there any way to do it faster or improve above code.

Thank You.
Posted
Updated 6-Jun-14 2:59am
v5

1 solution

C#
private async static void Search(string searchText, CloudBlockBlob blockBlob)
{
     string text = await blockBlob.DownloadTextAsync();
     if (text.IndexOf(searchText) != -1)
     {
          Console.WriteLine(blockBlob.Name);
     }
}


Downloading all text together in async mode maybe make faster.
 
Share this answer
 
v4

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