Get some folder properties inside Azure container to be used as trigger for Azure blob function

Muhammad Guruh Ajinugroho 105 Reputation points
2023-02-15T15:03:02.8466667+00:00

Hi,
I'm currently designing a blob trigger function that will send messages to some device clients. However there are several questions here. First, is there any way we could read a folder metadata like datemodified property so everytime there are changes inside the folder, and whenever the datemodified property gets updated, it will be used as the trigger. Second, is there any way we could read the folder structure to be able to generate conditions to compose the message to be sent to the client devices?
for example, let's say I have a folder structure like this in my container :

root-folder (folder structure)
     ---SUB_A
         ---1.2
              --release-notes.txt
              --SUB_A3.5_V1.2.23.0215.zip
         ---beta
              --release-notes.txt
              --SUB_A3.5V1.2.23.0214.zip
         ---1.3
              --release-notes.txt
              --SUB_A3.5_V5.3.23.0215.zip
     ---SUB_B
          ---1.2
              --release-notes.txt
              --SUB_B-v1.2.23.0215.zip
         ---beta
              --release-notes.txt
              --SUB_B-v1.2.23.0214.zip
         ---1.3
              --release-notes.txt
              --SUB_B-v1.3.23.0215.zip

Can I get the subfolders properties like folder's name (i.e. SUB_A , and then inside it : 1.2, beta, 1.3) inside the root-folder with the SDK so I could use it as a value?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,932 questions
Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,538 questions
{count} votes

Accepted answer
  1. Abdul Sajid Mohammed 470 Reputation points Microsoft Employee
    2023-02-15T19:33:43.71+00:00

    Hello @Muhammad Guruh Ajinugroho

    Thanks for posting your question here.

    You can use the Azure Blob Storage SDK to read the metadata of a blob or a container, including the datemodified property. Here's an example of how to get the datemodified property for a blob:

    Also, you can use the Azure Blob storage trigger for Azure Functions to read the folder metadata and structure in your container.

    To read the folder metadata, you can use the LastModified property of the BlobProperties class in the Azure Functions runtime. This property returns the date and time that the blob was last modified. You can use this property as the trigger for your function.

    To read the folder structure, you can use the path property in the trigger configuration. The path property allows you to specify a pattern for the blobs that you want to trigger your function. For example, you can use the following pattern to trigger your function only when a new file is added to the SUB_A folder:

    "path": "root-folder/SUB_A/{name}"

     

    In your function code, you can use the name variable to access the name of the blob that triggered the function. You can then use this information to generate the message to be sent to the client devices.

     

    Here's an example of how you can implement a Blob storage trigger function in Azure Functions which might need some customization based on your need:

    using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Extensions.Logging;
    using Microsoft.WindowsAzure.Storage.Blob;
    
    public static class BlobFunction
    {
        [FunctionName("BlobFunction")]
        public static void Run(
            [BlobTrigger("root-folder/SUB_A/{name}", Connection = "AzureWebJobsStorage")] CloudBlockBlob blob,
            string name,
            ILogger log)
        {
            log.LogInformation($"C# Blob trigger function processed blob\n Name:{name} \n Blob Size: {blob.Properties.Length} Bytes");
    
            // Read the folder metadata
            var lastModified = blob.Properties.LastModified;
    
            // Generate the message to be sent to the client devices
            var message = GenerateMessage(name, lastModified);
    
            // Send the message to the client devices
            SendMessage(message);
        }
    
        private static string GenerateMessage(string name, DateTime lastModified
    
    
    

    Reference Documents:

    https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/data-factory/how-to-create-event-trigger.md

    https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/azure-functions/functions-bindings-storage-blob-trigger.md

    Hope this helps, please let me if any questions.

    Please "Accept as Answer" and Upvote if it helped, so that it can help others in the community looking for help on similar topics. Thank you!


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.