0

I'm trying to get a blob-triggered Azure Function to work. I got the samples to run, and am all set up with my local development AzureWebJobsStorage connection string set up to use Azurite. I am testing by adding a file using Azure Storage Explorer.

This function triggers and my code is run:

@app.blob_trigger(arg_name="myblob", path="inbox",
                               connection="AzureWebJobsStorage") 
def BlobTriggerTest4(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob. "
                f"Name: {myblob.name}, "
                f"Blob Size: {myblob.length} bytes")

Log output in the VS Code terminal (size shows as zero in this code from the MS sample, which is fine, because stream has not yet been read):

[2024-09-13T09:15:37.534Z] Host lock lease acquired by instance ID '000000000000000000000000FF46044C'.
[2024-09-13T09:15:45.228Z] Executing 'Functions.BlobTriggerTest4' (Reason='New blob detected(LogsAndContainerScan): inbox/test.txt', Id=2bf0477b-06e1-4af3-af64-285dd0d65d20)
[2024-09-13T09:15:45.230Z] Trigger Details: MessageId: e46320bd-d339-49a7-af17-2be2def490d1, DequeueCount: 1, InsertedOn: 2024-09-13T09:15:45.000+00:00, BlobCreated: 2024-09-13T09:15:44.000+00:00, BlobLastModified: 2024-09-13T09:15:44.000+00:00

[2024-09-13T09:15:45.300Z] Python blob trigger function processed blob. Name: inbox/test.txt, Blob Size: None bytes

[2024-09-13T09:15:45.316Z] Executed 'Functions.BlobTriggerTest4' (Succeeded, Id=2bf0477b-06e1-4af3-af64-285dd0d65d20, Duration=140ms)

When I change it to use BlobClient, however, the function triggers buy my code isn't run! I want to use BlobClient so I can easily delete the blob once I have processed it.

@app.blob_trigger(arg_name="myblob", path="inbox",
                               connection="AzureWebJobsStorage") 
def BlobTriggerTest5(myblob: blob.BlobClient):
    logging.info(
        f"Python blob trigger function processed blob. Properties: {myblob.get_blob_properties()}.  Blob content head: {myblob.download_blob().read(size = 1)}"
    )

Here is the output. I get the boilerplate framework output but my own logging code doesn't run:

[2024-09-13T09:17:58.634Z] Host lock lease acquired by instance ID '000000000000000000000000FF46044C'.
[2024-09-13T09:18:02.117Z] Executing 'Functions.BlobTriggerTest5' (Reason='New blob detected(LogsAndContainerScan): inbox/test.txt', Id=af20f729-9414-4531-b22a-e75d9031b225)
[2024-09-13T09:18:02.120Z] Trigger Details: MessageId: 548b4b12-5af0-4b5e-b424-2fe7222c4908, DequeueCount: 1, InsertedOn: 2024-09-13T09:18:02.000+00:00, BlobCreated: 2024-09-13T09:18:00.000+00:00, BlobLastModified: 2024-09-13T09:18:00.000+00:00

Note that in this case with BlobClient I am not even getting the usual "Executed 'Functions.BlobTriggerTest5'" terminating message in the logs, almost as if the function has somehow got "stuck" right after triggering.

I had more code in these test functions previously, and none of it was running. I cut these examples down to more clearly demonstrate the problem.

I tried recreating the function from scratch in case the previous name had somehow been bound to the earlier implementation, etc. I can just switch back and forth between these two functions (only having one uncommented at a time since they trigger on the same blob path), and with the first using InputStream my function code runs and with the second using BlobClient it does not (despite the function showing it has been triggered!).

Can anyone suggest what I might be doing wrong or what is happening here? Many thanks in advance.

[Edit] I have now discovered that the BlobClient parameter version does indeed work when uploaded to Azure Functions, but it fails to run when triggered in the local debug environment. This means I can at least use it to access the blob, delete it, etc., but means I can't very easily debug it locally: I have to upload it and debug it there, using logging, etc., which is quite restrictive.

1 Answer 1

1

Use below code to delete the blob using BlobClient after processing it.

Function Code:

import azure.functions as func
from azure.storage.blob import *

app= func.FunctionApp()

Connectionstring="<Storage_Connection_string>"

@app.blob_trigger(arg_name="myblob", path="input/{name}.txt",
                               connection="BlobStorageConnectionString") 
def BlobTrigger(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob"
                f"Name: {myblob.name}")
    client = BlobServiceClient.from_connection_string(Connectionstring)
    container_client = client.get_container_client("ContainerName")
    blob_client = container_client.get_blob_client("filename.txt")
    
    //delete the blob
    blob_client.delete_blob()
    logging.info(f"Blob {myblob.name} deleted successfully")

Uploaded and Processed the blob in portal:

enter image description here

Deleted the blob using the code:

enter image description here

Console Output:

Functions:

        BlobTrigger: blobTrigger

For detailed output, run func with --verbose flag.
[2024-09-16T10:56:05.355Z] Host lock lease acquired by instance ID '000000000000000XXX31CC'.       
[2024-09-16T10:57:21.968Z] Executing 'Functions.BlobTrigger' (Reason='New blob detected(LogsAndContainerScan): input/Hello World.txt', Id=c489e4f4-540d-4XXXXX85f925ca5)
[2024-09-16T10:57:21.974Z] Trigger Details: MessageId: a95f2c1f-2055-XXXXX-93761a877dcc, DequeueCount: 1, InsertedOn: 2024-09-16T10:57:21.000+00:00, BlobCreated: 2024-09-16T10:57:21.000+00:00, BlobLastModified: 2024-09-16T10:57:21.000+00:00
[2024-09-16T10:57:22.245Z] Python blob trigger function processed blobName: input/Hello World.txt
[2024-09-16T10:57:22.267Z] Request URL: 'https://storageaccount.blob.core.windows.net/input/Hello%20World.txt'

Request method: 'DELETE'
Request headers:
    'x-ms-version': 'REDACTED'
    'Accept': 'application/xml'
    'User-Agent': 'azsdk-python-storage-blob/12.22.0 Python/3.11.9 (Windows-10-10.0.22631-SP0)'
    'x-ms-date': 'REDACTED'
    'x-ms-client-request-id': '7335e06c-741a-11ef-89e6-98fa9b0d1c80'
    'Authorization': 'REDACTED'
No body was attached to the request
[2024-09-16T10:57:22.651Z] Response status: 202
Response headers:
    'Content-Length': '0'
    'Server': 'Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0'
    'x-ms-request-id': '08fccebf-e01e-0047-0b27-08943a000000'
    'x-ms-client-request-id': '7335e06c-XXX89e6-98fa9b0d1c80'
    'x-ms-version': 'REDACTED'
    'x-ms-delete-type-permanent': 'REDACTED'
    'Date': 'Mon, 16 Sep 2024 10:57:23 GMT'
[2024-09-16T10:57:22.651Z] Blob input/Hello World.txt deleted successfully
[2024-09-16T10:57:22.762Z] Executed 'Functions.BlobTrigger' (Succeeded, Id=c489e4f4-540d-47c8-842f-39d85f925ca5, Duration=1237ms)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, @Pravallika, that works around the problem, but doesn't explain why a blob-triggered function using a BlobClient parameter fails locally. I am currently using an explicit BlobClient local variable to delete the blob, but whenever I try to use BlobClient or ContainerClient as a parameter to the function it seems it works fine in Azure but fails locally, with the trigger simply not firing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.