Creating an Azure Function with a Blob Trigger

Max Blum
5 min readJan 24, 2022

--

I never thought I would write an article, even less a tutorial here. So what changed? I struggled with a problem at work and could not find any appropriate tutorial for this Problem. Usually, when you look up any code-related issues, you find many solutions on StackOverflow, GitHub, or any other website. So I decided to write the tutorial myself, so here we go my first tutorial:

Creating an Azure Function with a Blob Trigger

This is relatively easy. You just need to understand the basics. For this tutorial, I decided to demonstrate a simple blob trigger. When the file is uploaded to container 1, it is copied to container 2. To explain it the best way possible in text form, I created a step-by-step tutorial.

Pre-requisites:

  • Azure Account
  • Azure Subscription
  • Visual Studio Code
  • Python installed

Step 1: Create an Azure Storage Account

The first step is to create a storage account on the Azure Portal. Therefore, you go to the marketplace and search “storage account”. Figure 1 displays the configuration site when you create the account. You have to choose the subscription and the resource group (Here, you can also create a new resource group). Then, you need to name the storage account and select your region.

Step 2: Create Containers

The second step is the creation of the two containers I mentioned above. Starting from the Azure Portal, you need to go to your storage account and click “Containers” on the left ribbon. Then you should see a similar page. Here you just need to click “+ Container” to add new containers to your storage account.

This sheet should appear on the right side. Here, you just need to name your container and choose your access level. Repeat the steps for container number 2 and remember the name of your two containers for later!

Step 3: Create the Azure Function App

Creating the function is the most crucial step here. To do that, you need to go to the Azure Marketplace and search for “Function App”. Again, you need to choose a subscription and a resource group and name it. Next, you need to select your runtime stack, the version and the region. I prefer Python and Germany. Now, you have created the Function App, BUT not the functions themselves!

Step 4: Create a Function

I decided to create the function directly in Visual Studio Code. Therefore, you need to have the Azure Tools Extension installed and signed into your Azure account. You switch to functions and press “Create New Project…” to make the function. Then, you need to create a new empty directory and select this directory.
Step 2 is to choose a language. Since I selected Python earlier, I need to choose Python again. Then you select an interpreter for Python. The interpreter needs to be installed beforehand.
Step 3 is to choose what kind of function you want to create here. You have to click “Azure Blob Storage trigger” and name it.
Step 4 is to select a “local.settings.json”. The easiest way is to create a new local app setting.
Step 5 is selecting the storage account. Select the storage account where you created your containers.
Step 6 is the monitoring. You need to specify the path of the container that should get monitored. It should be name-of-your-Container1/{name}. The name in the curly bracket is like a variable. If you upload a file to container 1, it gets copied into container 2 with the same name.
Congratulations, you have initialised your first Azure Function with a Blob Storage trigger.

Step 5: Choose the Target

In Step 5, you want to select the target container, which is the container you want to copy your file when you upload it in container 2. Initially, you only should see the first binding “inputblob”. However, it should be called myblob. I decided to rename it for better readability.

Therefore, you need to go to your functions.json file and right-click it. Then, you need to choose “add new binding”. First, you choose “out” as the direction, then select Azure blob storage and name it as you can see. I called it outputblob. The next step is to define the path to your outcontainer. In your case, this should be Container2/{name}. I added “-copy” to mark the copy version of the file. As connection, you choose the same as you did in your input path.

Step 6: Write the Code

Because this tutorial only copies a file from one container to another, the code here is quite simple. You just open __init__.py, where most of the text should be predefined. I changed it a little bit. But let’s get to the essential changes. First, you need to include the outputblob in the variables you give the function and adjust the name of your inputblob. Since I changed myblob to inputblob, I need to change that here. Second, you need to write the function. You can change the text however you like. If you want to mention the file name in the logging.info refer to it as inputblob.name. To copy the files when they get uploaded, you need to set the outputblob = inputblob.

import loggingimport azure.functions as func
def main(inputblob: func.InputStream, outputblob):logging.info(f"Python blob trigger function processed {inputblob.name}.")outputblob.set(inputblob)

Step 7: The End

Congratulations, you have done it. Now all you need to do is to test it. You can debug it locally by pressing F5. Your console should look like this if you upload a file via the Azure Portal to container 1.

--

--

Max Blum

Master Student Information Systems @Uni Hamburg | Junior Data Engineer @Beiersdorf Shared Services