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:
- Triggering a function when a new blob is created or updated.
- Reading the content of a blob as input to a function.
- Writing output from a function to a blob.
- Listing blobs in a container.
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"
}
]
}
connection property refers to an application setting name that holds your Azure Storage account connection string. Typically, this is set to AzureWebJobsStorage.
Common Scenarios
- Image Processing: Trigger a function when a new image is uploaded to blob storage, resize it, and save the resized version to another blob.
- Data Ingestion: Read CSV or JSON files from blob storage, process the data, and store it in a database or another storage service.
- Configuration Management: Load configuration settings from a blob file into your function's environment.
Advanced Features
Blob bindings support various options, including:
- Wildcard Paths: Use wildcards (e.g.,
samples-workitems/*.csv) to match multiple blobs. - Blob Name Parameters: Extract parts of the blob name into function parameters (e.g.,
{name}). - Connection Strings: Specify the connection string to use via application settings.
- Access Modes: For input bindings, you can specify
FileAccess.Read(default),FileAccess.Write, orFileAccess.ReadWrite.