Azure Storage Blob Listing Query with Search Pattern

Azure Storage blob is not behaving like files and folders in your local computer, albeit looking like one. So, there’s no support for search pattern, such as *.txt

The solution is to write your own search pattern.

from https://stackoverflow.com/questions/30299671/matching-strings-with-wildcard


public static String WildCardToRegular(String value) {
    return "^" + Regex.Escape(value).Replace("\\*", ".*") + "$";
}

Then, using it in Listing the blob:


var blobList = await container.ListBlobsSegmentedAsync(blobFilePath, true, BlobListingDetails.None, 1000, token, null, null);
var items = blobList.Results.Select(x => x as CloudBlockBlob);

// Filter items by search pattern, if specify
if (!string.IsNullOrEmpty(searchPattern))
{
    items = items.Select(i =>
    {
        var filename = Path.GetFileName(i.Name);
        if (Regex.IsMatch(filename, WildCardToRegular(searchPattern), RegexOptions.IgnoreCase))
        {
            return i;
        }
	return null;
    }).ToList();
}

Leave a comment