Search code examples
pythonazureazure-functionsazure-blob-storage

how to get the blob name with an azure function v2


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

@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.


Solution

  • 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()}")
    

    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.

    If you need additional assistance with accessing the uploaded blob content, share additional details of your use case.