1

I have problems reading the Content/Data with Python and the BlobTrigger. I use local environment and followed the documentation (https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=python). The function is listening and also fires successfully, when I upload a file to the local blob emulator. I also can get the filepath/filename in a variable, but can’t read the content of the uploaded file.

When I try to get the content, it always shows an empty string or array.

So this is my functions.json file:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "log/SystemLog/{name}",
      "connection": ""
    }
  ]
}

This is my init.py file: import logging import azure.functions as func

def main(myblob: func.InputStream):    
    
    print(myblob.name)
    print(myblob.length)
    print(myblob.readlines())

This is what get printed in the console:

[2021-10-29T07:27:25.053Z] Host lock lease acquired by instance ID '000000000000000000000000F86KCB51'.
[2021-10-29T07:27:25.124Z] Worker process started and initialized.
[2021-10-29T07:27:46.941Z] Executing 'Functions.BlobTriggerLocalTest' (Reason='New blob detected: log/SystemLog/testfile.txt', Id=3981bd58-accb-4c9c-b3e4-fe33b1a74522)
[2021-10-29T07:27:46.948Z] Trigger Details: MessageId: 7c575bad-88b7-46d4-b5bf-67b90fe0ab4d, DequeueCount: 1, InsertionTime: 2021-10-29T07:27:46.000+00:00, BlobCreated: 2021-10-29T07:27:43.000+00:00, BlobLastModified: 2021-10-29T07:27:43.000+00:00
[2021-10-29T07:27:47.032Z] log/SystemLog/testfile.txt
[2021-10-29T07:27:47.037Z] None
[2021-10-29T07:27:47.042Z] []
[2021-10-29T07:27:47.068Z] Executed 'Functions.BlobTriggerLocalTest' (Succeeded, Id=3981bd58-accb-4c9c-b3e4-fe33b1a74522, Duration=204ms)

I tried some solutions to get along with the problem.

First Thing was to go along with the documentation https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-input?tabs=python. But the changes in the functions.json files are not fully clearly for me. I tried to input

{
      "name": "inputblob",
      "type": "blob",
      "dataType": "binary",
      "path": "log/SystemLog/{name}",
      "connection": "",
      "direction": "in"
    },

in the functions.json file and added the inputblob as a second parameter to the main-method, but len(inputblob) also prints a 0.

I think I also could use and import the BlobServiceClient from azure.storage.blob. But I guess the BlobServiceClient would need a separate connection string and I want to avoid that.

2 Answers 2

1

The solution is simple: Do not use Visual Studio Code as File Explorer to upload per Drag&Drop.

The documentation to local environment lead me to use VS Code as File Explorer for the local blob. Uploading a file from Desktop to the blob resulting in loosing the content of the file. The file gets uploaded with the correct filename, but its empty inside. Using MS Storage Explorer solves the problem.

Sign up to request clarification or add additional context in comments.

Comments

-1

We can read the content from python code by using download_blob() function with adding content_as_text to it.

Below is the sample code:

import logging
import sys
import os
import azure.functions as func
from azure.storage import blob
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__

def main(myblob: func.InputStream):
    try:
        logging.info(f"Python blob trigger function processed blob \n")
        CONN_STR = "ADD_CON_STR"
        blob_service_client = BlobServiceClient.from_connection_string(CONN_STR)

        # MAP SOURCE FILE
        blob_client = blob_service_client.get_blob_client(container="newcontainer0805", blob="source.txt")

        #SOURCE CONTENTS
        content =  blob_client.download_blob().content_as_text
        print(content)

Later you can print the content as print(content).

1 Comment

Sure, but i would expect the input parameter "myblob" to contain the blob. I want to avoid loading it with a new connection.

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.