Search code examples
c#azuretagsazure-blob-storage

Azure Storage FindBlobsByTag possibility of ordering and taking n results


I've recently started using Azure Storage's FindBlobsByTagAsync, and I wonder if there's a way in which I can:

  1. take n results
  2. order the results so that I get the first or last n results

I'm using the following code to retrieve a subset of blobs by tag...

private async Task<IReadOnlyCollection<string>> GetBlobNamesByTagAsync(BlobContainerClient sourceContainer, int take)
{
    var taken = 0;
    var blobNames = new List<string>();

    await foreach (var taggedBlob in sourceContainer.FindBlobsByTagsAsync(...))
    {
        blobNames.Add(taggedBlob.BlobName);
        taken++;

        if (taken == take)
        {
            break;
        }
    }

    return blobNames;
}

... but does the blob-tagging API permit me to specify a take amount, or to specify that I want the (lexigraphically) first or last n blobs?


Solution

  • AFAIK, There is no direct class to achieve this. However, I tried sorting the tags, selecting the n tags and its respective blobs using the below code.

    using Azure;
    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var taken = 0;
            var blobNames = new List<string>();
            var TagNames = new Dictionary<string,string>();
            BlobContainerClient sourceContainer = new BlobContainerClient("<Connection String >", "container");
    
    
            foreach (var blob in sourceContainer.GetBlobs())
            {
                string blobName = blob.Name;
                BlobClient blobClient = new BlobClient($"<Connection String>", "container",blobName);
                var tags = blobClient.GetTags().Value.Tags;
                foreach (var tag in tags)
                {
                    TagNames[tag.Key] = tag.Value;
                }
            }
    
    
            var sortedTag = TagNames.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);
    
            Console.WriteLine("Enter the n value");
            string number = Console.ReadLine();
            int n = int.Parse(number);
            int ind = 0;
            
            foreach (var item in sortedTag)
            {
                if(ind<n)
                {
                    Console.WriteLine(item);
                    ind++;
                    foreach (var taggedBlob in sourceContainer.FindBlobsByTags($"{item.Key}='{item.Value}'"))
                    {
                        blobNames.Add(taggedBlob.BlobName);
                        Console.WriteLine(taggedBlob.BlobName);
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
    

    Results:

    enter image description here