4
votes

According to the Azure Blob storage bindings for Azure Functions documentation, when configuring a blob trigger you can utilize pattern matching on the blob name to map portions of the path to variables in the function eg.

[FunctionName("BlobTriggered")]        
public static void BlobTriggered(
    [BlobTrigger("myContainer/{name}.{extension}")] Stream myBlob,
    string name,
    string extension,
    TraceWriter log)
{
    // Given the blob path "myContainer/myBlob.png":
    // name == "myBlob"
    // extension == "png"
}

I have tested this and it works brilliantly for my use case, however due to the large delays in the BlobTrigger firing (often upwards of 5 minutes) it is not a viable option. As a result, I'm looking to make this an Event Grid trigger instead as per the suggestion from the Azure Functions scale and hosting documentation:

When you're using a blob trigger on a Consumption plan, there can be up to a 10-minute delay in processing new blobs. This delay occurs when a function app has gone idle. After the function app is running, blobs are processed immediately. To avoid this cold-start delay, use an App Service plan with Always On enabled, or use the Event Grid trigger.

Is there any way to get this same pattern matching behavior from an input binding instead of a trigger?

In my specific situation I have set up an EventGrid subscription for blob creation which runs an orchestrator function calling an activity function to read and parse the blobs:

[FunctionName("NewBlobCreated")]
public static async Task NewBlobCreated(
    [EventGridTrigger]EventGridEvent eventGridEvent,
    [OrchestrationClient]DurableOrchestrationClient starter,
    ILogger log)
{
    // Start our orchestrator function to read the file
    string instanceId = await starter.StartNewAsync(
        "OrchestrateBlobReader",
        eventGridEvent);
}

// Orchestrator function
[FunctionName("OrchestrateBlobReader")]
public static async Task OrchestrateBlobReader(
    [OrchestrationTrigger] DurableOrchestrationContext context,
    ILogger log)
{
    var eventGridEvent = context.GetInput<EventGridEvent>();
    var parsedBlob = await context.CallActivityAsync<string>("ReadBlob", eventGridEvent.Data);        
    ...
}

[FunctionName("ReadBlob")]
public static async Task<string> ReadBlob(
    [ActivityTrigger] JObject eventData,
    [Blob("{data.url}", FileAccess.Read)]CloudBlockBlob blob,
    ILogger log)
{
    using (var blobStream = await blob.OpenReadAsync())
    {
        // Blob is able to be read from blobStream here
        ...
    }
}

Ideally I would like for my ReadBlob function to behave similarly to the BlobTriggered function from the first example example above to do something along the lines of the following:

[FunctionName("ReadBlob")]
public static async Task<string> ReadBlob(
    [ActivityTrigger] JObject eventData,
    [Blob("{data.url}", FileAccess.Read)]CloudBlockBlob blob,
    string extension,
    ILogger log)
{
    if (extension.Equals("txt", StringComparison.OrdinalIgnoreCase))
    { ... }
    else if (extension.Equals("png", StringComparison.OrdinalIgnoreCase)
    { ... }
    else
    { ... }
}

The problem is I can't see any way to bind the extension parameter to the Blob input binding as I was doing for the BlobTrigger - expecially with the path being bound to the url provided by the EventGridEvent in the form of the eventData JObject.

Is it possible to achieve this same pattern matching functionality in this case? Or am I going to have to parse the path string myself to extract the relevant info?

1

1 Answers

5
votes

After looking through the source code for the blob trigger bindings - my "quick-and-dirty" solution was to tap into the underlying BindingTemplateSource class which the trigger uses to map the path & pattern to a dictionary.

Updated ReadBlob function is as follows:

// So we can access the BindingTemplateSource class
using Microsoft.Azure.WebJobs.Host.Bindings.Path;

[FunctionName("ReadBlob")]
public static async Task<string> ReadBlob(
    [ActivityTrigger] JObject eventData,
    [Blob("{data.url}", FileAccess.Read)]CloudBlockBlob blob,
    ILogger log)
{
    // Define the pattern to match
    var blobPattern = "myContainer/{name}.{extension}";
    // Create a BindingTemplateSource from the pattern string
    var patternTemplate = BindingTemplateSource.FromString(blobPattern);
    // Use this BindingTemplateSource to create the binding data
    // This returns a IReadOnlyDictionary<string, object> with the parameters mapped
    var parameters = patternTemplate.CreateBindingData($"{blob.Container.Name}/{blob.Name}");
    // Assuming blob path was "myContainer/myBlob.png":
    // Parameters are objects so we need to ToString() them
    var name = parameters["name"].ToString(); // name == "myBlob"
    var extension = parameters["extension"].ToString(); // extension == "png"

    if (extension.Equals("txt", StringComparison.OrdinalIgnoreCase))
    { ... }
    else if (extension.Equals("png", StringComparison.OrdinalIgnoreCase))
    { 
        // This executes now!
    }
    else
    { ... }
}

This functionality could then probably be wrapped in a custom binding where the parameters are mapped to output bindings on the function like the BlobTrigger does for the most elegant solution, but hacking it into the function like this achieves what I need for the short term