Blob Storage Bindings
Azure Functions provides triggers and bindings for Blob Storage, enabling you to easily interact with Azure Blob Storage from your serverless functions.
Overview
Blob Storage bindings allow your Azure Functions to:
- Trigger a function when a new or updated blob is created or deleted in a storage container.
- Read blob content as an input to your function.
- Write data to a blob as an output from your function.
Trigger: Blob Trigger
The Blob trigger starts your function when a blob is created or updated in a specified container.
Configuration
The Blob trigger is configured in the function.json file.
{
"scriptFile": "run.cs",
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "samples-workitems/{name}",
"connection": "AzureWebJobsStorage"
}
]
}
name: The name of the variable in your function code that will hold the blob content.path: The blob path to monitor.{name}is a wildcard that captures the blob name.connection: The name of an app setting that contains your Azure Storage connection string.
Input Binding: Blob Input
Use a Blob input binding to read the content of a blob into your function.
Configuration
{
"scriptFile": "run.cs",
"bindings": [
{
"name": "inputBlob",
"type": "blob",
"direction": "in",
"path": "samples-data/{fileName}",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputBlob",
"type": "blob",
"direction": "out",
"path": "samples-output/{fileName}.processed",
"connection": "AzureWebJobsStorage"
}
]
}
In this example, inputBlob will contain the content of the blob specified by {fileName}.
Output Binding: Blob Output
Use a Blob output binding to write data from your function to a blob.
Configuration
The Blob output binding is also configured in function.json.
{
"scriptFile": "run.cs",
"bindings": [
{
"name": "outputBlob",
"type": "blob",
"direction": "out",
"path": "samples-output/{outputFileName}",
"connection": "AzureWebJobsStorage"
}
]
}
In your code, you would assign data to the variable named outputBlob to write it to the specified blob path.
Supported Data Types
Blob bindings support various data types for reading and writing blob content:
| Binding Type | Type | Description |
|---|---|---|
| Trigger/Input | string |
Reads blob content as a string. |
| Trigger/Input | byte[] |
Reads blob content as a byte array. |
| Trigger/Input | Stream |
Reads blob content as a stream for efficient handling of large blobs. |
| Trigger/Input | ILogger |
Gets a logger object for the blob operation (often used with triggers). |
| Output | string |
Writes string content to a blob. |
| Output | byte[] |
Writes byte array content to a blob. |
| Output | Stream |
Writes stream content to a blob. |
| Output | TextWriter |
Writes text content to a blob, often used for generating CSV or log files. |
connection string is stored securely as an application setting in your Azure Functions configuration.
Examples
C# Example (Blob Trigger and Output)
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.IO;
public static class BlobProcessor
{
[FunctionName("BlobProcessor")]
public static void Run(
[BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")] Stream inputBlob,
string name,
[Blob("samples-output/{name}.processed", FileAccess.Write, Connection = "AzureWebJobsStorage")] Stream outputBlob,
ILogger log)
{
log.LogInformation($"C# Blob trigger function processed blob\n Name:{name} \n Size: {inputBlob.Length} Bytes");
// Read from input blob and write to output blob
inputBlob.CopyTo(outputBlob);
log.LogInformation($"Successfully processed and wrote to samples-output/{name}.processed");
}
}
JavaScript Example (Blob Trigger)
module.exports = async function (context, myBlob) {
context.log('JavaScript blob trigger function processed blob');
context.log('Name:', context.bindingData.name);
context.log('Blob Size:', myBlob.length, 'Bytes');
context.log('Content:', myBlob.toString());
// Example: Process the blob content and potentially write to another binding
// For this example, we'll just log it.
};
For more detailed information and advanced scenarios, please refer to the official Azure Functions Blob Storage documentation.