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 tr
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.