Using Microsoft Document Intelligence along with Azure Function Blob Trigger

Harsh Khewal 150 Reputation points
2024-04-29T06:32:48.6033333+00:00

Hi, I want to use a blob trigger with Microsoft's Document Intelligence. What should be the remaining code to analyze multiple documents I have stored in my blob storage container? Additionally, I also want the script to trigger any time I upload a new file to the container.

app = func.FunctionApp()

@app.blob_trigger(arg_name="myblob", path="input",
                               connection="mystorage_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")

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
2,100 questions
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 17,766 Reputation points Moderator
    2024-05-07T16:07:03.3633333+00:00

    Hi @Harsh Khewal Greetings! Thank you for posting your question on this forum.

    When you create an Azure function using the blob_trigger type, the function processes all the files which are already present in the container. The Azure function will also automatically trigger when new files are added to the linked container. You have to provide the container name in the path configuration of app.blob_trigger

    The document Get started with Document Intelligence provides a sample Python code that you can incorporate inside your blob_trigger function definition to perform document analysis on the files. The above code sample uses URL source to process the files. You can get this information using the Blob service client. Please refer the below sample

    import azure.functions as func
    from azure.storage.blob import BlobServiceClient
    import logging
    import os
    
    app = func.FunctionApp()
    
    
    # Create a BlockBlobService object
    blob_service_client = BlobServiceClient.from_connection_string(os.environ['lsayanastorage_STORAGE'])
    
    @app.blob_trigger(arg_name="myblob", path="testblobtrigger",
                                   connection="lsayanastorage_STORAGE") 
    
    
    
    def lsayanablob_trigger(myblob: func.InputStream):
        blob_client = blob_service_client.get_blob_client(container="testblobtrigger", blob=myblob.name)
        blob_url = blob_client.url
    
        
        #Include code from Document intelligent SDK 
        #feed the above blob_url to fromURL in analyze_layout()
    
        logging.info(f"Python blob trigger function processed blob"
                    f"Name: {myblob.name}"
                    f"Blob URL is {blob_url}"
                    f"Blob Size: {myblob.length} bytes")
    

    Hope this helps. Please let us know if you need any additional assistance or further clarification.


    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

0 additional answers

Sort by: Most helpful

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.