liveBook · Manning

Azure Functions binding

As explained in Microsoft Azure in Action

An Azure Functions binding is a declarative configuration that connects a function to an input or output data source or service.

A trigger starts an Azure Function; a binding connects it to data it needs to read or write. Input and output bindings expose connected resources through function parameters, avoiding boilerplate for those connections. A function has exactly one trigger, but it can use bindings for its other work. section 8.2.2 section 8.6

Resize an image and save the result

An HTTP-triggered function can read an NFT image from one Blob Storage container and write a smaller copy to another. Its input binding opens the original as a readable stream; its output binding supplies a writable stream. Image-resizing code still does the resizing—the bindings handle access to the blobs. section 8.2.2

[FunctionName("ProcessNFT")]
public static void Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, 
"get", "post", Route = "nft/{nftid}")] HttpRequest req,    #1
    [Blob("originalnfts/{nftid}", FileAccess.Read)] 
Stream nftImage,    #2
    [Blob("copynfts/{nftid}", FileAccess.Write)] 
Stream nftSmall)    #3
{
    IImageFormat format;

    using (Image<Rgba32> input = 
Image.Load<Rgba32>(nftImage, out format))    #4
    {
        input.Mutate(x => x.Resize(320, 200));    #5
        input.Save(nftSmall, format);    #6
    }
}

The listing places an HTTP trigger and two Blob bindings on the function’s parameters. The input uses FileAccess.Read, the output uses FileAccess.Write, and the route’s {nftid} appears in both blob paths. section 8.2.2

Bindings can also be configured in the Azure portal. For a blob-triggered image function, add an Azure Blob Storage output, select the storage-account connection, name its parameter outputBlob, and set a destination such as imageoutputcontainer/{rand-guid}. The path places each resized image in the output container with a randomly generated GUID filename. The Integration view shows the resulting flow into and out of the function. section 2.2.19

Choose a binding for the resource interaction, not for the processing itself. Bindings can connect functions to inputs and outputs such as blobs, queues, or databases; the function code remains responsible for transforming the data. section 2.5 section 8.2.2

How Microsoft Azure in Action covers it

How deep it goes
Shows portal output settings and a worked C# input/output example, but does not explain binding runtime internals.
Examples
Uses a portal-configured resized-image output and a C# function that reads an NFT image and writes a smaller blob.
What it assumes
Assumes a function and trigger are already in place, then introduces bindings as the way to access other resources.
What you can do after
Operate/configure it in the Azure portal or apply it in C# to read and write Blob Storage from a function.
How it connects
Links bindings to Azure Functions triggers, Blob Storage, HTTP APIs, and image processing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microsoft Azure in Action. The section links point to the parts of the book it draws on.