Create Azure Blob Storage Trigger to execute Azure Function

Amit kumar
5 min readJan 7, 2021

In this article, I will explain steps to create Azure Blob Storage Trigger to execute Azure Function. The Blob storage trigger starts Azure Function when a new file is uploaded. The Blob (uploaded file) contents are provided as input to the function. Azure Function reads the file content and map it to the XML format DTO. Ultimately it creates new XML document using DTO and Blob Storage output binding is configured to post generated XML files to another Blob storage container.

Why doing this Serverless?

Serverless computing is a model, which provides developers with multi-functioning tools, allowing to create top-notch apps more effectively.

Azure Function is a powerful serverless cloud platform, based on an Event-Driven Architecture allowing you to run code on-demand, with Microsoft taking care of the whole infrastructure and backend system.

Some of the benefits of doing this using Azure Functions are

1- Event-driven using triggers and bindings

2- Cost effective as it will use consumption-based plan

3- Easy to build and debug locally without additional setup and

4- Operate at scale in the cloud.

Now, You need an Azure Account and can use Visual Studio or Visual Studio Code for this. You must also login to your Azure Portal in Visual Studio. For this article, I have used Visual studio 2019 and .Net core 3.1 on Windows 10.

1. Creating Azure Function App

In Azure, Functions are hosted in an execution context called a Function App. You define function apps to logically group and structure your functions and a compute resource in Azure.

On Azure Functions tab, select ‘Create a function app in Azure’ with the options below. You can select any other configuration which suits for you. The Azure Function App requires a general-purpose storage account. So, it will also create a storage account for us.

Resource group- TestAzureFunctionAppRG2020

FunctionApp- TestAzureFunctionApp2020

After entering all the details same as above screenshots, click next. On next screen Create new storage account.

Storage account- testfunctionstorage2020

And select Plan Type — Consumption (Serverless).

Click “review + create”, It will create your function App.

2. Creating Storage Container for application files

The next step is creating two containers in storage account. For this article, I created two containers: first “samples-json-files” where I will upload the Json files and second where Azure Function will save the generated XML files.

Make sure you create two seperate container for generated XML files as menioned. If you will save XML in the same folder as the input json, you’ll trigger an infinite loop.

3. Create Azure Function project in Visual Studio

Azure Function is the serverless computing service hosted on the Microsoft Azure public cloud. Azure Function, and serverless computing, in general, is designed to accelerate and simplify application development.

We can create Azure Function in Azure portal as well as in IDEs like Visual Studio or Visual Studio Code. In this article, we will create Azure Function in Visual Studio and publish it on Azure.

Open Visual Studio and Create a new Azure Function project.

Microsoft provides several templates like HTTPTrigger, TimerTrigger, BlobTrigger, CosmosDBTrigger, etc. You can use BlobTrigger template when you want the function code to execute every time a new blob is added to an Azure Storage account.

Select BlobTrigger.

Now replace sample Azure Function code with below in your newly created BlobTrigger. You need to change the Azure Storage container names if you have created it with different names.

In this Azure Function, to keep it simple, I have done the DTOs mapping manually. But for complex mapping, we can use AutoMapper.

4. Publish Azure Function to Azure

Disclaimer: I am publishing from Visual Studio for the sake of expediency here, as it’s not something I recommend doing. The preferred method to deploy is via a CI/CD pipeline.

Right click on project in Visual Studio and select Publish.

It will ask to sign in with Azure account if you are not all signed in.

Next, It will list all your Function Apps from your account. Select desired Function App and click finish.

While publishing, you need to configure dependencies including Azure Storage account. While configuring storage account, you can choose to keep connection string in Azure App string or Key vault.

It will include all dependencies in zip as shown in below summary.

5. Test Blob Function Trigger

Create json file with below sample json:

{“id”: 1,“FirstName”: “testFirstName”,“LastName”: “testLastName”,“DateOfBirth”: “01/11/1998”,}

And upload it on your Blob container `samples-json-files`

The Upload will trigger the Azure Function and it will create a new output XML file with same name in your output container `sample-xml-files`

You can reach the codes in this post from my GitHub repository.

--

--