locked
Pattern search in Azure blob file names RRS feed

  • Question

  • I am trying to search files that match my search pattern.
    static void ListBlobsInFolder() {
                var account = <MyStorageAccount>
                var blobClient = account.CreateCloudBlobClient();
                var containerName = "importcontainer";
                var folderName = "subfolder";
                var container = blobClient.GetContainerReference(containerName);
                var query = container.ListBlobs(prefix: folderName, useFlatBlobListing: true, blobListingDetails: BlobListingDetails.None);
                foreach(var item in query) {
                    Console.WriteLine(item.Uri.Segments.Last());
                }
            }

    This gets me the list of files present in the subfolder.

    Is there a way to return the files that only match a pattern. Example I want to retrieve file names that contains "abc" in it.

    Is this possible in Azure ?

    Wednesday, October 31, 2018 5:26 AM

All replies

  • Hi Thanga,

    You should be able to use a LINQ query for getting the results of files with a specific pattern. You can use boolean method Contains("abc").  For example:

    if(item.Uri.Segments.Last().Contains("abc"))
    {
        Console.WriteLine(item);
    }

    For further details on how you can use this method, I would recommend the following LINQ reference guide: https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.contains?view=netframework-4.7.2

    If this answer was helpful, click “Mark as Answer” or Up-Vote. To provide additional feedback on your forum experience, click Here


    • Proposed as answer by robrien-MSFT Wednesday, October 31, 2018 10:30 AM
    Wednesday, October 31, 2018 10:30 AM
  • Thanks for the reply. 

    If I understand correctly, this brings everything from the server and applying the filter in client side. I don't want to do this. Is there a way to filter the records from the server side itself ?


    Thursday, November 1, 2018 4:15 AM
  • Hi Thanga,

    Yes, you are right, this is filtered on the client side.

    You can also use the "prefix" parameter in the ListBlobs() method to filter the results. This will be done on the server side and will return a list of blobs that begin with a specific word or letter.

    For example, you can use the prefix "c" which will return all blobs that begin with the letter "c".

    If this answer was helpful, click “Mark as Answer” or Up-Vote. To provide additional feedback on your forum experience, click Here

    Thursday, November 1, 2018 1:01 PM