Blob Input Bindings
Blob input bindings allow your Azure Function to read data from Azure Blob Storage as input. This can be used to access configuration files, data payloads, or any other data stored in blobs.
Key Benefit: Simplifies data access from Blob Storage, allowing you to focus on your function's logic rather than the storage interaction details.
How it Works
When you define a blob input binding in your function's configuration (e.g., function.json or using attributes in C#), Azure Functions runtime handles the retrieval of the blob content. The content is then provided to your function as a parameter.
Configuration
function.json Example
{
"bindings": [
{
"name": "myBlob",
"type": "blob",
"direction": "in",
"path": "samples-data/my-file.json",
"connection": "AzureWebJobsStorage"
}
],
"disabled": false
}
Attribute-based (C#) Example
using Microsoft.Azure.WebJobs;
public static class BlobTriggerFunction
{
public static void Run(
[BlobTrigger("samples-data/my-file.json", Connection = "AzureWebJobsStorage")] Stream myBlob,
string name)
{
// Function logic here
}
}
Key Properties:
name: The name of the parameter in your function code that will receive the blob content.type: Must be"blob".direction: Must be"in"for input bindings.path: The path to the blob within your storage account. This can include wildcards.connection: The name of the app setting that contains your Azure Blob Storage connection string.
Supported Data Types
The blob content can be bound to various data types in your function, depending on your needs:
byte[]: The entire blob content as a byte array.string: The entire blob content as a string.Stream: A readable stream of the blob content.CloudBlockBlob(or equivalent SDK type): Provides access to the Blob Storage SDK object for more advanced operations.- Custom POCOs (Plain Old CLR Objects) or JSON objects: If the blob content is JSON, it can be automatically deserialized.
Working with Different Paths
The path property is powerful and supports wildcards for flexible blob selection:
samples-data/{name}: Binds to a blob namedmy-file.jsonin thesamples-datacontainer. The valuemy-file.jsonis passed to a parameter namedname.samples-data/{*filePath}: Binds to any blob within thesamples-datacontainer. The entire blob path (e.g.,my-folder/my-file.txt) is passed to a parameter namedfilePath.
Example: Reading JSON Data
This example demonstrates reading a JSON file and deserializing it into a C# object.
config.json (in Blob Storage)
{
"setting1": "value1",
"setting2": 123
}
C# Function
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.IO;
public class Settings
{
public string setting1 { get; set; }
public int setting2 { get; set; }
}
public static class ReadSettingsFunction
{
public static void Run(
[Blob("config/config.json", Connection = "AzureWebJobsStorage")] Settings appSettings,
ILogger log)
{
if (appSettings != null)
{
log.LogInformation($"Setting1: {appSettings.setting1}");
log.LogInformation($"Setting2: {appSettings.setting2}");
}
else
{
log.LogError("Failed to load app settings.");
}
}
}
Error Handling
If the specified blob does not exist, the function might not be triggered or may result in an error, depending on the binding configuration and language runtime. It's good practice to check if the input parameter is null or handle potential exceptions.
Tip: Use a container name and a specific blob name for predictable input. Use wildcards carefully and ensure your function can handle cases where the blob might not be present.