Azure Functions Blob Bindings

Blob bindings allow your Azure Functions to interact with Azure Blob Storage. You can use them for both input and output operations, enabling your functions to read from and write to blobs without writing explicit storage SDK code.

Overview

Blob bindings simplify common scenarios involving blob storage, such as:

Note: Blob bindings are supported for various languages, including C#, JavaScript, Python, Java, and PowerShell. Configuration details might vary slightly between languages.

Input Bindings

Input bindings allow you to read the content of a blob directly into a function parameter.

Blob Trigger

The blob trigger allows a function to execute automatically when a blob is created or updated in a specified container. The blob content is often passed to the function as an input stream or byte array.

Example (C#)

C#

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public static class BlobTriggerFunction
    {
        [Function("BlobTriggerCSharp")]
        public static void Run(
            [BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob,
            string name,
            ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
            // You can read from 'myBlob' stream here.
        }
    }
}
            

Example (JavaScript)

JavaScript

module.exports = async function (context, myBlob) {
    context.log("JavaScript blob trigger function processed blob");
    context.log(`Name: ${context.bindingData.name}`);
    context.log(`Size: ${myBlob.length} Bytes`);
    // 'myBlob' contains the blob content.
};
            

Blob Input Binding

This binding reads the content of a specific blob into a function parameter before the function executes.

Example (C#)

C#

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public static class BlobInputFunction
    {
        [Function("BlobInputCSharp")]
        public static void Run(
            [BlobTrigger("input-container/{name}", Connection = "AzureWebJobsStorage")] string myBlob, // Reads blob as string
            [Blob("config-container/settings.json", Connection = "AzureWebJobsStorage")] Stream configBlob, // Reads config blob as stream
            string name,
            ILogger log)
        {
            log.LogInformation($"Blob content: {myBlob}");
            // Use 'configBlob' for configuration settings.
        }
    }
}
            

Output Bindings

Output bindings allow your function to write data to a blob.

Blob Output Binding

This binding enables your function to write data to a blob in Azure Blob Storage. The data can be passed as a string, byte array, or stream.

Example (C#)

C#

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System.IO;

namespace Company.Function
{
    public static class BlobOutputFunction
    {
        [Function("BlobOutputCSharp")]
        [Blob("output-container/{rand-guid}-output.txt", FileAccess.Write, Connection = "AzureWebJobsStorage")]
        public static Stream Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestData req,
            ILogger log)
        {
            string responseMessage = "This is the output content written to blob storage.";
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(responseMessage);
            var stream = new MemoryStream(byteArray);
            log.LogInformation("Writing output to blob.");
            return stream; // The returned stream will be written to the blob
        }
    }
}
            

Example (JavaScript)

JavaScript

module.exports = async function (context, req) {
    const outputBlobContent = "This is some content to be written to a blob.";
    context.bindings.outputBlob = outputBlobContent; // Assign content to the output binding

    context.res = {
        body: "Blob content successfully written."
    };
};
            

Configuration

Blob bindings are configured in the function.json file (for Node.js, Python, etc.) or using attributes in code (for C#, Java).

function.json Example (Input)

JSON

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "input-container/{name}",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputBlob",
      "type": "blob",
      "direction": "out",
      "path": "output-container/{name}.out",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
            

function.json Example (Output)

JSON

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ],
      "authLevel": "function"
    },
    {
      "name": "outputBlob",
      "type": "blob",
      "direction": "out",
      "path": "myoutputblobs/output.txt",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ]
}
            
Important: The connection property refers to an application setting name that holds your Azure Storage account connection string. Typically, this is set to AzureWebJobsStorage.

Common Scenarios

Advanced Features

Blob bindings support various options, including:

See Also