Introduction:
In this post, we will focus on the Azure Blob Storage trigger, which is a specific type of trigger used in data processing scenarios. Azure Functions is a powerful serverless computing platform that can execute code in response to various triggers, such as HTTP requests, timer events, or messages from a message queue. We will guide you through the process of building an Azure Function that automatically reads and processes Excel files uploaded to Azure Blob Storage using an input binding.
Demo
Section 1:
Setting up the Azure Function Project In this section, we’ll walk you through the process of creating an Azure Function project in Visual Studio, and setting up the necessary dependencies for our project, including the Microsoft.Azure.WebJobs.Extensions.Storage
and EPPlus
NuGet packages.
Section 2:
Creating the Azure Function In this section, we’ll walk you through the process of creating the Azure Function that will handle the Blob Storage trigger and read the Excel files. We’ll start by creating a function with the BlobTrigger
attribute that specifies the path to the container and the name of the blob that should trigger the function. Then, we’ll use the EPPlus
library to open the Excel file, extract the data from the sheet, and log the data to the Azure Functions output.
Section 3:
Defining the Input Binding In this section, we’ll show you how to define the input binding for the Blob Storage trigger in the function.json
file. We’ll specify the path
to the container and the {name}
of the blob that should trigger the function, and also specify the name of the app setting that contains the connection string to the storage account.
here’s an example of an Azure Function with an input binding that uses C# to read an Excel file from Azure Blob Storage and process its contents.
First, you’ll need to add the Microsoft.Azure.WebJobs.Extensions.Storage
and EPPlus
NuGet packages to your project.
Here’s an example function code that reads the Excel file from blob storage, extracts the data from the sheet, converts it into a model, and logs it:
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using OfficeOpenXml;
[FunctionName("BlogTriggerFunction")]
public void Run([BlobTrigger("%ContainerName%", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
using (var package = new ExcelPackage(myBlob))
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var worksheet = package.Workbook.Worksheets[0];
var rowCount = worksheet.Dimension.Rows;
var columnCount = worksheet.Dimension.Columns;
for (int row = 2; row <= rowCount; row++)
{
var phoneNumber = worksheet.Cells[row, 1].Value?.ToString();
var firstName = worksheet.Cells[row, 2].Value?.ToString();
var lastName = worksheet.Cells[row, 3].Value?.ToString();
var address = worksheet.Cells[row, 4].Value?.ToString();
var groupName = worksheet.Cells[row, 5].Value?.ToString();
var model = new MyModel
{
PhoneNumber = phoneNumber,
FirstName = firstName,
LastName = lastName,
Address = address,
GroupName = groupName
};
log.LogInformation($"Processed row {row - 1}: {model}");
}
}
}
public class MyModel
{
public string PhoneNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string GroupName { get; set; }
public override string ToString()
{
return $"{{PhoneNumber={PhoneNumber},FirstName={FirstName}, LastName={LastName}, Address={Address}, GroupName={GroupName}}}";
}
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=accnamehere;AccountKey=XrcmloA0NMtUvdc9Gj+wKFRhQ80/VmvRH1ZZrto7A1t7eEa345345345xsyFgwrL+AStVyQ9YA==;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"ContainerName": "samples-workitems/{name}"
}
}
In this example, the BlobTrigger
attribute specifies the name of the container and the {name}
parameter specifies the name of the blob that triggered the function. The Connection
property specifies the name of the app setting that contains the connection string to the storage account.
The function uses the ExcelPackage
class from the EPPlus
package to open the Excel file stream and extract the data from the first sheet. It then loops through each row in the sheet, extracts the values of the PhoneNumber
, Address
, and GroupName
columns, and creates a new instance of the MyModel
class with the extracted data. Finally, it logs the model to the Azure Functions output.
You’ll need to create a function.json
file that defines the input binding for the blob trigger:
{
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "mycontainer/{name}",
"connection": "AzureWebJobsStorage"
}
]
}
In this example, the path
property specifies the same container and blob name pattern as in the BlobTrigger
attribute in the function code. The connection
property specifies the name of the app setting that contains the connection string to the storage account.
Application Source Code @ LearnSmartCoding GitHub
Check out other topics that might interest you.
- Automating API Requests with C# and Azure Functions using HttpTrigger.
- Getting Started with Azure Durable Functions: An Example of Function Chaining
Conclusion:
We have shown you how to build an Azure Function with input binding that reads and processes Excel files uploaded to Azure Blob Storage in this post. With this setup, you can automate the processing of data from Excel files, and use the power of Azure Functions to scale your data processing capabilities.