Home > database >  How do I get the file name that triggered a Azure Function Blob Trigger
How do I get the file name that triggered a Azure Function Blob Trigger

Time:01-18

I have a Azure function with a blob trigger in Python that scans the content of of PDF files that get added to a container, how do I get the file name of the file that triggered the trigger e.g. "bank_data.pdf"?

def main(myblob: func.InputStream):
   blob = {myblob.name}

I get this error when trying to get the name through the InputStream:

Result: Failure Exception: FunctionLoadError: cannot load the pdf_blob_trigger_test function: the following parameters are declared in Python but not in function.json: {'name'} Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 371, in _handle__function_load_request self._functions.add_function( File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/functions.py", line 353, in add_function input_types, output_types = self.validate_function_params(params, File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/functions.py", line 137, in validate_function_params raise FunctionLoadError(

function.json

{
"scriptFile": "__init__.py",
"bindings": [
    {
        "name": "myblob",
        "type": "blobTrigger",
        "direction": "in",
        "path": "data-upload-pdf-test/{name}.pdf",
        "connection": "AzureWebJobsStorage",
        "containerName": "data-upload-pdf-test"
    }
  ]
}

Solution to my problem is in the comments of the accepted answer

CodePudding user response:

I have reproduced in my environment and got expected results as below:

Firstly, go to Function App, I have used Logs section to find which file is triggered my Azure Blob Trigger by using below KQL query:

traces 
| where message contains "Python blob trigger"

enter image description here

In Storage Account:

enter image description here

init.py

import logging
import azure.functions as func
def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")

enter image description here

So, you can able to log the details of file name in Logs section of function app. So, used f"Name: {myblob.name}\n" to print file name which triggered.

  • Related