Is there any way to optimize my search blob program?
- by Vicky
I written 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.
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)
{
string line = string.Empty;
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 blobContainer.ListBlobs(); is blocking code because search will not work until all the blob items loaded. Is there anyway to optimize it or anywhere else in my code.
Thanks