Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating names for output blobs for an Azure Function

Using the binding options for an Azure Function one can specify the name of a Blob to be written based on parameters derived from the trigger (e.g. the queue message that triggered the function); the documentation shows an example of that.

My question is: what is the best way to handle the case where the blob name is not known in advance, but in fact is calculated as part of the function's execution?

And related: what to do if the function may or may not produce an output blob (or multiple output blobs!), based on the outcome of its calculation?

As far as I can see now Azure Function's binding mechanism doesn't help much in these cases and the easiest approach is to refer to an assembly that does the azure blob writing "the classical way". But is there a more idiomatic way?

like image 728
Luc C Avatar asked Apr 05 '16 13:04

Luc C


People also ask

What is blob trigger Azure function?

The Blob storage trigger starts a function when a new or updated blob is detected. The blob contents are provided as input to the function. There are several ways to execute your function code based on changes to blobs in a storage container.

What is function JSON in Azure function?

The function. json file defines the function's trigger, bindings, and other configuration settings. Every function has one and only one trigger. The runtime uses this config file to determine the events to monitor and how to pass data into and return data from a function execution.

How do I create a blob storage trigger?

Create an Azure Blob storage triggered functionSelect Functions, and then select + Add to add a new function. Choose the Azure Blob Storage trigger template. Use the settings as specified in the table below the image. Name of this blob triggered function.


1 Answers

You can actually already to this in C# Azure Functions, and we have a tracking item here in our repo to enable this for Node.js Functions as well. We'll get to that soon.

Below is an example working function that binds to a blob with the path specified at runtime. Since under the covers Azure Functions is built on the Azure WebJobs SDK, you'll notice that this relies on using the WebJobs SDK Binder something that you might not be familiar with. Please see the WebJobs SDK for more documentation on IBinder/Binder. In the WebJobs SDK, declarative attributes are used for bindings (e.g. QueueAttribute/TableAttribute/BlobAttribute, etc.). You can specify all of these at runtime via Binder. In Azure Functions, we use external metadata to describe bindings, but in this advanced scenario you have a hybrid. Note that when using Binder there is no corresponding binding in function.json. For more details on Binder dynamic bindings see this SO question/answer.

In general, you'll find that many awesome WebJobs SDK features are usable in Azure Functions - our doc just needs to catch up to make people aware of this :)

One other thing to note: there is some inbuilt support for generating random new identifiers for outputs. E.g. if you were to set your output blob path to test-output/{rand-guid} the system will automatically generate a new ID for you. If that meets your needs, then you don't need Binder.

using System;
using System.IO;
using System.Net;
using Microsoft.Azure.WebJobs;

public static async Task<HttpResponseMessage> 
       Run(HttpRequestMessage req, Binder binder, TraceWriter log)
{
    log.Verbose($"C# HTTP function processed RequestUri={req.RequestUri}");

    using (var writer = await binder.BindAsync<TextWriter>(
                  new BlobAttribute("test-output/result")))
    {
        writer.Write("Hello World!!");
    }

    return new HttpResponseMessage(HttpStatusCode.OK);
}

For your second question, if you want to conditionally write to an output binding, just don't assign any value to the binding - no output should be produced.

like image 199
mathewc Avatar answered Oct 20 '22 06:10

mathewc