Azure Functions Blob Trigger Binding

The Blob Trigger allows an Azure Function to be executed automatically when a new or updated blob is detected in Azure Blob Storage.

Introduction

This binding is incredibly useful for event-driven architectures, enabling you to process files as they are uploaded to storage. Common use cases include image processing, data ingestion pipelines, and file validation.

Binding Attributes

When defining a blob trigger, you'll use specific attributes in your function's configuration (e.g., function.json or attributes in code).

Common Attributes:

Trigger Behavior

The blob trigger monitors the specified path in Blob Storage. When a blob is created or modified that matches the defined path, the function is invoked.

Blob Processing:

Configuration

Configuration can be done in two primary ways:

1. Using function.json (for JavaScript, Python, PowerShell, etc.):

{
  "scriptFile": "../run.py",
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "samples-workitems/{name}.csv",
      "connection": "AzureBlobStorageConnectionString"
    }
  ]
}

2. Using Attributes in Code (for C#, Java, TypeScript):

C# Example:

using Microsoft.Azure.WebJobs;

            public static class BlobTriggerFunction
            {
                public static void Run(
                    [BlobTrigger("samples-workitems/{name}.csv", Connection = "AzureBlobStorageConnectionString")] Stream myBlob,
                    string name)
                {
                    // Function logic here
                    // myBlob is the stream of the blob content
                    // name is the name of the blob (e.g., "myfile")
                }
            }

TypeScript Example:

import { AzureFunction, Context, BlobTrigger } from "@azure/functions";

            const blobTrigger: AzureFunction = async function (
                context: Context,
                myBlob: Buffer // Or 'string' or 'NodeJS.ReadableStream'
            ): Promise {
                context.log("Blob trigger function processed blob");
                context.log(`Name: ${context.bindingData.name}`);
                context.log(`Size: ${myBlob.length} Bytes`);
                // Your processing logic here
            };

            export default blobTrigger;
Note: The name property in the path (e.g., {name}.csv) will be available in the function's context (context.bindingData.name in JavaScript/TypeScript) and can be used to access specific parts of the blob name.

Examples

Processing a CSV file and inserting data into a database:

When a .csv file lands in the data-input container, the function is triggered, reads the CSV content, and processes each row.

Resizing images upon upload:

A blob trigger on an images container can invoke a function that resizes the uploaded image and saves the new version to a different container or blob path.

Best Practices

Tip: For very large files, consider using blob triggers with a Stream or binary data type and processing the data in chunks to manage memory usage.