How to use env variable for path argument in blob trigger python

Maria Dąbrowiecka 80 Reputation points
2025-04-04T09:40:08.1033333+00:00

Hi team,

I'd like to define the path to the blob from env variable in my blob trigger function:

import logging

import azure.functions as func
import azurefunctions.extensions.bindings.blob as blob


app = func.FunctionApp()

@app.blob_trigger(arg_name="client",
                  path="PG_DUMP_CONTAINER_NAME",
                  connection="PG_DUMP_CONTAINER_CONNECTION_STRING",
                  run_on_startup=False
                  )
def function(client: blob.BlobClient, context: func.Context):
    logging.info(
        f"Python blob trigger function processed blob \n"
        f"Properties: {client.get_blob_properties()}"
    )

it doesn't work. I need to hardcode the value of the path. On the other hand connection argument is taken from env variable and it works. Are there any limitations?

Thanks for your help,
Maria

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Vinodh247 34,661 Reputation points MVP Volunteer Moderator
    2025-04-04T11:03:48.1233333+00:00

    Hi ,

    Thanks for reaching out to Microsoft Q&A.

    Yes, you are running into a known limitation in azure functions.

    In the @blob_trigger, the path parameter does not support env variable substitution, unlike the connection parameter which does. That is why the connection string is working fine from your environment variable, but path="PG_DUMP_CONTAINER_NAME" is not being resolved.

    Why?

    • connection is handled as a configuration setting and resolved by the azure functions runtime.
    • path is treated as a literal string at the time the function bindings are processed. There is no interpolation from environment variables or app settings in the path parameter.

    Workaround

    Unfortunately, you must hardcode the container and blob path in the path parameter like this:

    @app.blob_trigger(arg_name="client",
                      path="my-container-name/{name}",
                      connection="PG_DUMP_CONTAINER_CONNECTION_STRING")
    
    

    If you really need to make it dynamic:

    Hack (not recommended for prod loads)!

    You could try:

    Generating the function code dynamically using Python before startup.

    Or using a generic Event Grid trigger to listen to blob events, and handle paths in code.

    Example using Event Grid trigger:

    @app.function_name(name="BlobEventGridTrigger")
    @app.event_grid_trigger(arg_name="event")
    def blob_event_handler(event: func.EventGridEvent):
        blob_url = event.get_json()["data"]["url"]
        # Use blob_url to dynamically process the blob
    
    
    
    1. path does not support env vars, limitation of Azure Functions.
    2. connection does support env vars.
    3. You must hardcode the path or switch to using Event Grid triggers for dynamic scenarios.

    Please feel free to click the 'Upvote' (Thumbs-up) button and 'Accept as Answer'. This helps the community by allowing others with similar queries to easily find the solution.


  2. RithwikBojja 3,055 Reputation points Microsoft External Staff Moderator
    2025-04-07T10:47:50.8066667+00:00

    Hi @Maria Dąbrowiecka,

    You can use environment variable in path by using % as below:

    local.settings.json:

    
    {
    
      "IsEncrypted": false,
    
      "Values": {
    
        "FUNCTIONS_WORKER_RUNTIME": "python",
    
        "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    
        "AzureWebJobsStorage":"storageconnectionstring",
    
        "containerName":"mycontainer"
    
      }
    
    }
    
    

    function_app.py:

    
    import azure.functions as func
    
    import logging
    
    app = func.FunctionApp()
    
    @app.blob_trigger(arg_name="myblob", path="%containerName%",
    
                                   connection="rithwik82ba_STORAGE") 
    
    def blob_trigger(myblob: func.InputStream):
    
        logging.info(f"Python blob trigger function processed blob"
    
                    f"Name: {myblob.name}"
    
                    f"Blob Size: {myblob.length} bytes")
    
    

    Here used path as %pathvariable%.

    Output:

    After Uploading a blob:

    enter image description here

    Function gets Triggered:

    enter image description here

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.