Blob Output Bindings
This document explains how to configure and use blob output bindings in Azure Functions. Blob output bindings enable your function to write data directly to Azure Blob Storage without requiring you to write explicit storage SDK code.
Overview
When you define a blob output binding for your Azure Function, the function runtime automatically handles the creation and writing to the specified blob. This simplifies your code and reduces boilerplate related to Azure Storage interactions.
Configuration
Blob output bindings are configured in the function.json file for your function. The following attributes are important:
- Type: Must be blobfor a blob output binding.
- Direction: Must be out.
- Name: The name of the parameter in your function code that will receive the blob.
- Path: The path to the blob within the storage container. This can include parameters from your function trigger.
- Connection: (Optional) The name of an app setting that contains the Azure Storage connection string. If omitted, the default connection string (AzureWebJobsStorage) is used.
Example function.json
                
{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "outputBlob",
      "type": "blob",
      "direction": "out",
      "path": "output-container/{name}.txt",
      "connection": "MyStorageConnectionAppSetting"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ]
}
                In this example:
- outputBlobis the name of the output binding.
- The data will be written to a blob named {name}.txtinside theoutput-container. The{name}part is a token that will be replaced by a value from the trigger or other bindings (e.g., an HTTP request query parameter).
- The connection string is specified in the MyStorageConnectionAppSettingapp setting.
Using the Binding in Code
The parameter defined in function.json (e.g., outputBlob) is available in your function code. You can assign data to this parameter to write to the blob.
C# Example
using System.Net.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
public static class BlobOutputFunction
{
    [FunctionName("BlobOutputFunction")]
    public static async Task Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req,
        [Blob("output-container/{Query.name}.txt", FileAccess.Write, Connection = "MyStorageConnectionAppSetting")] IAsyncCollector outputBlob,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        string name = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
            .Value;
        if (name == null)
        {
            name = "World";
        }
        string responseMessage = $"Hello, {name}! This HTTP triggered function executed successfully. Writing to blob...";
        await outputBlob.AddAsync($"Hello from Azure Function, {name}!");
        return new HttpResponseMessage(System.Net.HttpStatusCode.OK)
        {
            Content = new StringContent(responseMessage)
        };
    }
}
                 JavaScript Example (Node.js)
module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? 'Hello, ' + name + '! This HTTP triggered function executed successfully. Writing to blob...'
        : 'This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.';
    // Using the output binding (context.bindings.outputBlob)
    if (name) {
        context.bindings.outputBlob = `Hello from Azure Function, ${name}!`;
    } else {
        context.bindings.outputBlob = `Hello from Azure Function, World!`;
    }
    context.res = {
        status: 200,
        body: responseMessage
    };
};
                Dynamic Paths
The path property in function.json is powerful. You can use tokens like {name}, {rand-guid}, or expressions derived from trigger data. This allows you to create dynamic file names and folder structures.
Examples of Dynamic Paths:
- my-output/{rand-guid}.json: Creates a blob with a unique GUID as its name in the- my-outputcontainer.
- logs/{datetime:yyyy/MM/dd}/{rand-guid}.log: Organizes logs into a date-based folder structure.
- data/{id}.csv: If your trigger provides an- idproperty, the blob name will be based on that ID.
Note: Ensure that the container specified in the Path exists in your Azure Blob Storage account. Functions will not automatically create containers.
Common Scenarios
- Logging: Writing function execution logs or application-specific data to a blob.
- Data Processing: Storing results of data transformations or aggregations.
- File Archiving: Saving input files or intermediate results for auditing or future processing.
Important: For large data writes, consider using the IAsyncCollector<T> in C# or streaming capabilities in other languages to avoid loading the entire content into memory.
Error Handling
If an error occurs during the writing process, the function will typically fail, and an exception will be logged. Ensure your connection strings are correct and the storage account is accessible.