Blob Storage Bindings

Azure Functions provides robust integration with Azure Blob Storage, allowing you to seamlessly read from and write to blobs as part of your function execution. Blob storage bindings simplify common patterns like processing file uploads or responding to blob creation events.

Trigger: Blob Trigger

The Blob trigger starts your function when a new or updated blob is detected in a specified container. This is ideal for event-driven scenarios, such as image processing or data transformation upon file upload.

Configuration

The Blob trigger is configured using the path property in your function.json or via attributes in code.

function.json
{
  "scriptFile": "run.cs",
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "samples-workitems/{name}",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

In this example:

Input Binding: Blob Input

You can use a blob input binding to read the content of a blob directly into your function's parameters. This can be used in conjunction with other triggers or bindings.

function.json
{
  "scriptFile": "run.cs",
  "bindings": [
    {
      "name": "inputBlob",
      "type": "blob",
      "direction": "in",
      "path": "samples-data/{filename}",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputBlob",
      "type": "blob",
      "direction": "out",
      "path": "samples-processed/{filename}",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Output Binding: Blob Output

The blob output binding allows your function to write data to a blob. This is commonly used to save results, create reports, or store processed data.

function.json
{
  "scriptFile": "run.cs",
  "bindings": [
    {
      "name": "inputBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "samples-input/{name}",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputBlob",
      "type": "blob",
      "direction": "out",
      "path": "samples-output/{name}.processed",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Supported Data Types

Blob bindings can bind to various .NET types, including:

Tip: For complex scenarios or when you need more control, consider using the Azure Storage SDK directly within your function code. The bindings provide a convenient abstraction for common use cases.

Example: Processing a Text File

Here's a C# example demonstrating a blob trigger that reads a text file, processes its content, and writes to an output blob.

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

public static class BlobProcessor
{
    [FunctionName("ProcessBlob")]
    public static void Run(
        [BlobTrigger("samples-input/{name}", Connection = "AzureWebJobsStorage")] Stream inputBlob,
        [Blob("samples-output/{name}.processed", FileAccess.Write, Connection = "AzureWebJobsStorage")] Stream outputBlob,
        string name,
        ILogger log)
    {
        log.LogInformation($"C# Blob trigger function processed blob\n Name:{name} \n Size: {inputBlob.Length} Bytes");

        using (var reader = new StreamReader(inputBlob))
        using (var writer = new StreamWriter(outputBlob))
        {
            string content = reader.ReadToEnd();
            // Simulate processing: Convert to uppercase
            string processedContent = content.ToUpper();
            writer.Write(processedContent);
            log.LogInformation($"Processed content written to samples-output/{name}.processed");
        }
    }
}

Common Scenarios

Explore the Azure Functions documentation for more advanced features and configurations.