How to configure a blob trigger to only trigger for files beginning with specified substring [Python V2 Programming Model]

Luke Osborn 5 Reputation points
2024-03-15T15:33:40.3833333+00:00

Hi,

(Please note, I am using the V2 Python programming model).

I am trying to configure a blob trigger within a function app which only triggers when a new blob with a filename beginning with a certain substring. I have attempted to follow the documentation from the following, however it isn't working as it describes it should (it is instead triggering on ALL blobs, regardless of the file name): https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=python-v2%2Cisolated-process%2Cnodejs-v4&pivots=programming-language-python#blob-name-patterns

I have tried the following:

@app.blob_trigger(arg_name="blob",
                  path="container-name/sub-folder-name/my-substring-{name}.csv",
                  connection="CONNECTION_STRING")
def new_trigger(blob: func.InputStream):
    logging.info(f"Processing blob: {blob.name}")
    process_blob(blob)

My understanding is that this should trigger for all new blobs added to container-name/sub-folder-name/ with a file name of format: my-substring-<ANYTHING-HERE> (e.g. it should trigger for my-substring-123.csv but NOT different-substring-123.csv).

Is there anything I am missing here?

Thanks,

Luke

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2024-03-20T00:21:36.6366667+00:00

    Hey @Luke Osborn

    The trigger will match your pattern: my-sub-string-. So, anything starting with that will get matched. It won't match my-substring. You'll need to remove that second - from your pattern for that.

    Are you saying it triggers on different-substring-123.csv?


  2. Pinaki Ghatak 5,600 Reputation points Microsoft Employee Volunteer Moderator
    2024-05-03T08:40:21.04+00:00

    Hello @Luke Osborn

    There might be a few things that you can check to ensure that the trigger is working as expected:

    1. Make sure that the connection string you are using is correct and that it has the necessary permissions to access the blob storage account.
    2. Check if the container and sub-folder names are correct and that the blob you are trying to trigger on is located in the correct sub-folder.
    3. Ensure that the file name format is correct. The documentation specifies that the file name should start with the specified substring, so make sure that the file name starts with "my-substring-" and that there are no typos or extra characters. If you have checked all of the above and the trigger is still not working as expected, you can try adding a filter to the trigger to explicitly specify the file name pattern. Here is an example of how you can modify your code to add a filter:
    import re @app.blob_trigger(name='myblob', 
    			path='container-name/sub-folder-name/{name}', connection='CONNECTION_STRING')
    def new_trigger(blob: func.InputStream): 
    if re.match(r'^my-substring-', blob.name): 
    	logging.info(f'Processing blob: {blob.name}') process_blob(blob)
    

    In this example, we are using the re module to match the file name pattern explicitly. The re.match() function checks if the file name starts with my-substring- and returns a match object if it does.

    If there is a match, the trigger will process the blob. I hope this helps in your journey.


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.