Blob Input Bindings

Blob input bindings allow your Azure Function to read data from Azure Blob Storage as input. This can be used to access configuration files, data payloads, or any other data stored in blobs.

Key Benefit: Simplifies data access from Blob Storage, allowing you to focus on your function's logic rather than the storage interaction details.

How it Works

When you define a blob input binding in your function's configuration (e.g., function.json or using attributes in C#), Azure Functions runtime handles the retrieval of the blob content. The content is then provided to your function as a parameter.

Configuration

function.json Example


{
  "bindings": [
    {
      "name": "myBlob",
      "type": "blob",
      "direction": "in",
      "path": "samples-data/my-file.json",
      "connection": "AzureWebJobsStorage"
    }
  ],
  "disabled": false
}
                

Attribute-based (C#) Example


using Microsoft.Azure.WebJobs;

public static class BlobTriggerFunction
{
    public static void Run(
        [BlobTrigger("samples-data/my-file.json", Connection = "AzureWebJobsStorage")] Stream myBlob,
        string name)
    {
        // Function logic here
    }
}
                

Key Properties:

Supported Data Types

The blob content can be bound to various data types in your function, depending on your needs:

Working with Different Paths

The path property is powerful and supports wildcards for flexible blob selection:

Example: Reading JSON Data

This example demonstrates reading a JSON file and deserializing it into a C# object.

config.json (in Blob Storage)


{
  "setting1": "value1",
  "setting2": 123
}
                

C# Function


using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.IO;

public class Settings
{
    public string setting1 { get; set; }
    public int setting2 { get; set; }
}

public static class ReadSettingsFunction
{
    public static void Run(
        [Blob("config/config.json", Connection = "AzureWebJobsStorage")] Settings appSettings,
        ILogger log)
    {
        if (appSettings != null)
        {
            log.LogInformation($"Setting1: {appSettings.setting1}");
            log.LogInformation($"Setting2: {appSettings.setting2}");
        }
        else
        {
            log.LogError("Failed to load app settings.");
        }
    }
}
                

Error Handling

If the specified blob does not exist, the function might not be triggered or may result in an error, depending on the binding configuration and language runtime. It's good practice to check if the input parameter is null or handle potential exceptions.

Tip: Use a container name and a specific blob name for predictable input. Use wildcards carefully and ensure your function can handle cases where the blob might not be present.