Setting up a Python Function App with VSCode to configure Blob Metadata stored in an Azure Storage Account

As a follow up to my previous post:

Setting Azure Storage Account Blob Metadata with Python Script
https://blog.terenceluk.com/2024/12/setting-azure-storage-account-blob-metadata-with-python-script.html

This post serves to be part 2 where I will be converting the Python Script to a Function App so we can run this in Azure and be able to call it via tools such as Postman or a Logic App.

Developing a Function App in Python has changed quite a bit over the years and there were times I had to revert back to my notes to remember how to do it so I want to take this opportunity to walk through from start to finish for how to set up a local environment with VSCode, test it, and finally publish it to an Azure Function App.

Step #1 – Set up your local laptop or desktop for Azure Function with Python Development

Begin by downloading and install the following:

Virtual Studio Code: https://code.visualstudio.com/Download

Python: https://www.python.org/downloads/
**Note: Make sure you check to see what Python version VSCode currently supports as I made the mistake to install 3.13.0 only to find it was not supported so ended up reverting back to 3.9.12.

Here are a few screenshots of the settings chosen during the install:

Select Customize installation and select Add Python 3.9 to PATH:

Select the following Optional Features:

Select Install for all users in the Advanced Options:

Proceed to install Python:

The install should complete successfully:

You should see Python39 installed into your C:\Program Files\Python39 directory:

You should also see the Path set in your Windows environment variables:

While in VSCode, you should be able to execute python –version to display the installed version:

Python Extension for VSCode: https://marketplace.visualstudio.com/items?itemName=ms-python.python

Azure Extension for VSCode: https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions

Node.js: https://nodejs.org/en

Azurite Emulator: https://marketplace.visualstudio.com/items?itemName=Azurite.azurite

Step 2 – Create and Start Building and Azure Function on local laptop or desktop in VSCode

With the required applications and extensions let up, let’s proceed to create an Azure Function locally. Continue by creating a folder for where you want to store your Azure Function:

C:\Users\tluk\Documents\Azure Functions\Set-Blob-Metadata

Then open the folder within VSCode:

Proceed to click on the Azure (Ctrl+Shift+A) icon on the left navigation bar, click on the Azure Function at the bottom, then select Create Function:

Select the folder we’ll be developing the Azure Function App in:

Select Python:

Select Model V2:

We’ll select the locally installed Python version we installed earlier 3.9.12:

There are numerous triggers you available to execute this Function App. For the purpose of our Function, we’ll be using HTTP trigger:

The default name http_trigger will be filled out for the name of the function:

We’re going to change the name to setblobmetadatafunc:

We are now presented with the type of authorization level for calling the Function App. The details of each option can be found here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cfunctionsv2&pivots=programming-language-python#http-auth

We’ll proceed to select FUNCTION, which requires an API Key:

You should now see a new Azure Function with multiple files created:

With the new Function App created, let’s proceed to launch the terminal and activate a virtual environment by using the bundled .venv\Scripts\activate:

.venv\scripts\activate

More information about virtual environments can be found here: https://docs.python.org/3/library/venv.html

With the virtual environment activated, we can proceed to install the python packages required to run our Function App that will set the metadata for a blob. Navigate to the requirements.txt file and paste the following packages (azure-functions should already be in there):

azure-functions
azure-identity
azure-storage-blob

Save the file and run: pip install -r requirements.txt to install the packages in the virtual environment:

Next, we’ll need to edit the local.settings.json and add line to instruction the function to use the Azurite local storage emulator:

 

{
  “IsEncrypted”: false,
  “Values”: {
    “AzureWebJobsStorage”: “UseDevelopmentStorage=true”,
    “FUNCTIONS_WORKER_RUNTIME”: “python”
  }
}

Now proceed to verify Azurite local storage emulator service is running by clicking on View > Command Palette… (Ctrl+Shift+P):

Then in the type in the following in the command palette: Azurite: Start and click on the item:

Once successfully started, you should see __azurite files created in the main directory:

The Function App is now ready to be ran locally. Without updating the code, you should be able to start the function app by clicking Run > Start Debugging:

————————————————————————————————————————————————————————————————————

Note: I never had any luck using Run Without Debugging as it would always throw the error message:

Visual Studio Code
Invalid message: “noDebug” is not supported for “attach”

Clicking on Open ‘launch.json’ will direct me to the file in the .vscode folder:

The Function App will continue to run and successfully launch but I avoid this by just using Start Debugging.

Feel free to leave a comment if anyone knows how to get this to work without an error.

————————————————————————————————————————————————————————————————————

You’ll see in the console indicating that the requirements.txt packages were installed and the function successfully launches and begins listening:

We can try navigating to the URL in our browser:

You can also add ?=name=terence at the end of the endpoint to see the function app execute the block to say hello:

We have now successfully validated the Function App works.

Step 3 – Add Custom Python Code for Blob Metadata Configuration

Let us now proceed to add the custom Python code that will perform the following:

  1. Listen for HTTP requests
  2. When an HTTP request is received, take the provided JSON payload and set the metadata for a blob

Here is a sample JSON payload the function app accepts:

{
  “storage_account_url”: “https://fileuploadtest.blob.core.windows.net/”,
  “container_name”: “sharepoint”,
  “blob_path”: “Shared Documents/Root Folder/Folder01/Folder01 A”,
  “blob_file_name”: “Test01.docx”
}
Paste the code into function_app.py:

Before running the function app, we will need to authenticate into Azure with an account that has the Storage Blob Data Contributor role because the function app uses the

credential = DefaultAzureCredential() to configure the blob metadata tag. When this function app is deployed into Azure, we’ll be using a System Managed Identity to authenticate but for the local environment, we’ll need to authenticate with an account. The easiest way to do this is execute az login and log in with the account:
Now that we’re authenticated, we can start and run the function app to verify it can successfully ran in the local environment:
You can also try browsing to the URL and confirm that the following error is presented without providing the expected payload:
An error occurred: HTTP request does not contain valid JSON data
The console should also display the error:
Let’s now try to use Postman to test with a JSON payload:
{
  “storage_account_url”: “https://fileuploadtest.blob.core.windows.net/”,
  “container_name”: “sharepoint”,
  “blob_path”: “Shared Documents/Root Folder/Folder01/Folder01 A”,
  “blob_file_name”: “Test01.docx”
}
If the account authenticated with az login has the appropriate permissions to the blob, the function app should successfully execute:
The blob metadata should be set:
The console should also display the successful output:

Step 4 – Deploy Function App to Azure

With the local tests complete, we can proceed to deploy the Function App to Azure by clicking on the Azure icon (Ctrl+Shift+A), navigate to the desired subscription, expand Function App, and select the Function App you have pre-created in the subscription, right click and select Deploy to Function App:

The Function App should successfully deploy and it should be displayed in the Azure Portal:
Notice the Code + Test tab allows you to view the code but indicates: This function has been edited through an external editor. Portal editing is disabled.
Before we begin to test, we’ll need to make sure we turn on the System assigned Managed Identity for this Azure Function and grant it Storage Blob Data Contributor access to the storage account so it can write the metadata:
Grant the required permission:
Proceed to click on Get function URL in the headings and copy the default (Function Key):
Proceed and use the URL in Postman to test the Azure Function and you should see it complete successfully:
This Azure Function App is now available for you to use from a Logic App or another application to set the metadata for a blob. Feel free to adjust the script as one of the thoughts I have is to update it so the tags are not defined in the script but rather defined in the payload and used in the script so you’re not limited to the tags I’ve specified. Hope this helps anyone who may be looking for a start to finish configuration of deploy a Function App to Azure.
******************* Update ***********************
I’ve created a version 2 of this Function App Python script that will allow additional custom tags that can be added in the JSON payload. Here is a sample payload example:
{
“storage_account_url”: “https://fileuploadtest.blob.core.windows.net/”,
“container_name”: “sharepoint”,
“blob_path”: “Shared Documents/Root Folder/Folder01/Folder01 A”,
“blob_file_name”: “Test01.docx”,
“tag1”: “value1”,
“tag2”: “value2”,
“tag3”: “value3”
}