Azure Functions Blob Trigger (Python)

This document details how to use the blob storage trigger for Azure Functions with Python. The blob trigger allows your function to run automatically in response to changes in a blob container.

When to use the blob trigger

The blob trigger is ideal for scenarios where you need to process blobs as they are created or updated in Azure Blob Storage. Common use cases include:

Creating a Blob Trigger Function

To create a blob trigger function in Python, you define a function that accepts a BlobTrigger object as input. You specify the connection string and the blob container path in the function's binding configuration.

function.json Configuration

The bindings for your Azure Function are typically defined in a function.json file. For a blob trigger, it looks like this:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "samples-workitems/{name}",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
            

Let's break down the properties:

Python Code (__init__.py)

Your Python function code will receive the blob data as a stream or a string, depending on how you configure it. Here's a basic example:

import logging
import azure.functions as func

def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob\\n"
                 f"Name: {myblob.name}\\n"
                 f"URI: {myblob.uri}\\n"
                 f"Size: {myblob.length} Bytes")

    # Read the blob content
    blob_content = myblob.read().decode('utf-8')
    logging.info(f"Blob content: {blob_content[:200]}...") # Log first 200 chars

            

In this code:

Working with Blob Data

The func.InputStream object provides methods to interact with the blob content:

Blob Trigger Path Patterns

You can use wildcards and binding expressions in the path property to make your trigger more flexible:

For example, to trigger only for .csv files in a specific container:

"path": "input-csv/{name}.csv"
            

To trigger for blobs in a container named by an App Setting:

"path": "{inputcontainer}/processed/{name}"
            

And in your Python code, you can access these bound parameters:

import logging
import azure.functions as func

def main(myblob: func.InputStream, name: str, inputcontainer: str):
    logging.info(f"Blob '{name}' in container '{inputcontainer}' processed.")
    # ... rest of your logic

            

Error Handling and Retries

Azure Functions provides built-in retry mechanisms. If your function execution fails, the Functions runtime may retry the execution. For the blob trigger, this typically means the function will be invoked again with the same blob. Ensure your function is idempotent to handle retries gracefully.

Important: For production scenarios, especially with large files or complex processing, consider using the blob output binding to write results to a different location or service instead of modifying the input blob directly.

Related Topics