Azure Functions Documentation

Blob Storage Output and Input Bindings for Azure Functions

This document explains how to use Blob Storage bindings in Azure Functions to connect your functions to Azure Blob Storage. These bindings allow your functions to easily read from or write to blobs without requiring explicit SDK code.

Note: Blob Storage bindings simplify interactions with Blob Storage. For more advanced scenarios or direct manipulation, you can use the Azure Blob Storage SDK within your function.

Output Bindings

An output binding allows your function to write data to a blob. When you configure an output binding, you specify the blob's path and content. The function runtime handles the creation and upload of the blob.

Configuration

You configure output bindings in your function.json file (for JavaScript, Python, and PowerShell) or via attributes in your code (for C#).

Example function.json for an output binding:

json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myBlobOutput",
      "type": "blob",
      "direction": "out",
      "path": "samples-output/output-{name}.txt",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

In this example:

Usage in Code

Python Example:

python
import logging
import azure.functions as func

def main(req: func.HttpRequest, myBlobOutput: func.Out[str]) -> None:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        output_message = f"Hello, {name}. This blob was generated by an Azure Function."
        myBlobOutput.set(output_message)
        logging.info(f"Blob written to output with content: {output_message}")
    else:
        logging.warning("No name provided in request.")

Input Bindings

An input binding allows your function to read data from a blob. The runtime automatically fetches the blob content and makes it available to your function.

Configuration

Example function.json for an input binding:

json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myBlobInput",
      "type": "blob",
      "direction": "in",
      "path": "samples-input/input.txt",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

In this example:

Usage in Code

Python Example:

python
import logging
import azure.functions as func

def main(myBlobInput: str, myBlobOutput: func.Out[str]) -> None:
    logging.info("Python blob trigger function processed blob")
    logging.info(f"Blob content: {myBlobInput}")

    output_message = f"Content from input blob: {myBlobInput}"
    myBlobOutput.set(output_message)
    logging.info(f"Blob written to output with content: {output_message}")

Using Blob Triggers

Blob triggers are a special type of input binding that executes a function when a new or updated blob is detected in a specified container. The blob content is passed directly to your function as an input parameter.

Example function.json for a blob trigger:

json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myBlobTrigger",
      "type": "blobTrigger",
      "direction": "in",
      "path": "samples-trigger/{name}.txt",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "myBlobOutput",
      "type": "blob",
      "direction": "out",
      "path": "samples-processed/processed-{name}.txt",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

The myBlobTrigger parameter in your function will contain the content of the newly created or modified blob.

Common Scenarios

Tip: Ensure your connection string setting (e.g., AzureWebJobsStorage) is correctly configured in your Azure Functions application settings.