how to get the blob name with an azure function v2

jim01011 0 Reputation points
2024-09-04T18:43:52.6033333+00:00

How do I get a blob name from an azure function v2?

@app.function_name(name="PropertyExtract") @app.blob_trigger(arg_name="myblob", path="input/{name}",connection="AzureWebJobsStorage") @app.blob_input(arg_name="inputblob", path="input/{name}",connection="AzureWebJobsStorage") def main(myblob: func.InputStream,inputblob: str):

myblob.name only seems to work in console ? I want to store the blob name in a variable.

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

1 answer

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 15,811 Reputation points Microsoft Employee
    2024-09-04T22:12:39.43+00:00

    Hi @jim01011 Greetings! Welcome to Microsoft Q&A forum. Thank you for posting this question here.

    The blob name is available in the Azure blob trigger function V2 within the def main(myblob: func.InputStream) method and can be accessed outside of the logging.info as well.

    Looking at the method signatures in your function, you are trying to perform some operations on the newly added blob and trying to use the input binding through the @app.blob_input(arg_name="inputblob", path="input/{name}" function. Instead of this, you can create a blob_clinet under the method def main(myblob: func.InputStream) and perform operations or access blob data as follows

    def lsayanablob_trigger(myblob: func.InputStream):
    
        #feed the above URL to fromURL in analyze_layout()
        #Include code from Document intelligent SDK 
        logging.info(f"Python blob trigger function processed blob"
                    f"Name: {myblob.name}"
                    f"Blob Size: {myblob.length} bytes")
        #assign to variable
        blob_name = myblob.name.split("/")[-1]
        container_name = myblob.name.split("/")[0] 
        #create a blob service client
        newblob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
        logging.info(f"Printing the copied blob name {blob_name}")
        #extract the blob properties 
        logging.info(f"logging blob properties {newblob_client.get_blob_properties()}")
    

    Please note that the name filed accessed through myblob.name contains the path to the container and the name of the file. We can split that and extract the container name as well as file name to create the blob service client.

    With the created blob_service_client you can access all the methods outlined in the BlobClient class and perform various operations.

    Hope this helps! If you need additional assistance with accessing the uploaded blob content, kindly share additional details of your use case so we can assist you further.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    0 comments No comments

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.