Home > Back-end >  How to get metadata of a blob from Event Grid Trigger in Azure Function
How to get metadata of a blob from Event Grid Trigger in Azure Function

Time:02-13

My requirement is to get metadata from the blob on an EventGridTrigger. The ask seems to be simple, however I am not able to find examples online. Below is my Azure Funtion. I need to get the Metadata of the blob. What is the best way of getting it.

        public static void Run([EventGridTrigger] EventGridEvent eventGridEvent,
        [Blob("{data.url}", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream input,
        ExecutionContext context,
        ILogger log)
        {
              // other lines of code
              IDictionary<string, string> metadata = get_metadata_of_the_blob;
              // other lines of code
        }

I need to somehow get the Metadata within the function code at: get_metadata_of_the_blob

CodePudding user response:

try the following:

public static async Task Run(JObject eventGridEvent, CloudBlockBlob blob, ILogger log)
{
   // ...

   await blob.FetchAttributesAsync();           
   log.LogInformation($"\nMetadata: {string.Join(" | ", blob.Metadata.Select(i => $"{i.Key}={i.Value}"))}");

   // ...
}

and the function.json:

{
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "eventGridEvent",
      "direction": "in"
    },
    {
      "type": "blob",
      "name": "blob",
      "path": "{data.url}",
      "connection": "AzureWebJobsStorage",
      "direction": "in"
    }
  ],
  "disabled": false
}

CodePudding user response:

First you need a blog instance then you can get the blog meta data.

try
    {
        BlobClient blob = new ....
        // Get the blob's properties and metadata.
        BlobProperties properties = await blob.GetPropertiesAsync();

        Console.WriteLine("Blob metadata:");

        // Enumerate the blob's metadata.
        foreach (var metadataItem in properties.Metadata)
        {
            Console.WriteLine($"\tKey: {metadataItem.Key}");
            Console.WriteLine($"\tValue: {metadataItem.Value}");
        }
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
    }
  • Related