0
votes

Here are the problem details:

  • We are able to upload a file to Azure Blob Storage.
  • We also have an Azure Function that runs a Python (python 3.6) App (engine/init.py) using an http trigger (The function essentially takes a file and runs it through an Azure Speaker Recognition service)
  • We can run the code on a local machine and it works.

The problem is when we move the code to the Azure cloud (configured as a Linux Machine) and want to access the file in Blob Storage the app doesn't work (returns error 500). We have verified the path is correct (both source and destination), we can see the file in the blob storage, but we cannot read the contents of the file in Blob Storage (ie we cannot read the text in a .txt file) and we cannot copy or download the file into the Azure Function directory.

The file access details are in a file called downloadFile.py (see code below) which uses BlockBlobService to connect with the Azure Blob Storage.

  • We use block_blob_service.list_blob to find the file
  • We use block_blob_service.get_blob_to_path to download the file

Both functions work fine on the local machine as we have the azure-storage-blob library included in our requirements.txt file.

Ultimately, our objective is we need the python App to access a file in Blob storage and return a result. We are open to how we achieve that. Thx in advance for your help.

Here is the requirements.txt file contents:

azure-functions
azure-storage-blob == 1.5.0

Here is our code (downloadFile.py):

from azure.storage.blob import BlockBlobService, PublicAccess
import os, uuid, sys

def run_sample():
    try:
        # Create the BlockBlockService that is used to call the Blob service for the storage account
        block_blob_service = BlockBlobService(account_name='', account_key='')
        # Create a container called 'quickstartblobs'.
        #container_name ='quickstartblobs'
        container_name ='images'
        #block_blob_service.create_container(container_name)

        # Set the permission so the blobs are public.
        #block_blob_service.set_container_acl(container_name, public_access=PublicAccess.Container)

        # Create a file in Documents to test the upload and download.

        __location__ = os.path.realpath(
        os.path.join(os.getcwd(), os.path.dirname(__file__)))
        #local_path = os.path.join(__location__, 'SoundsForJay/')
        local_path = os.path.join(__location__)
        # not able to download file to azure function.


        #local_path=os.path.abspath(os.path.curdir)

        # List the blobs in the container
        print("\nList blobs in the container")
        generator = block_blob_service.list_blobs(container_name)
        for blob in generator:
            print("\t Blob name: " + blob.name)
            if ".wav" in blob.name:
                local_file_name = blob.name

        # Download the blob(s).
        # Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.

        #full_path_to_file2 = os.path.join(local_path, str.replace(local_file_name ,'.txt', '_DOWNLOADED.txt'))
        full_path_to_file2 = os.path.join(local_path, str.replace(local_file_name ,'.wav', '_DOWNLOADED.wav'))
        print("\nDownloading blob to " + full_path_to_file2)
        block_blob_service.get_blob_to_path(container_name, local_file_name, full_path_to_file2)

        sys.stdout.write("Sample finished running. When you hit <any key>, the sample will be deleted and the sample "
                         "application will exit.")
        sys.stdout.flush()
        #input()

        # Clean up resources. This includes the container and the temp files
        #block_blob_service.delete_container(container_name)
        #os.remove(full_path_to_file)
        #os.remove(full_path_to_file2)
    except Exception as e:
        print(e)
    return "run_sample is running."
1
can you try writing the file to /tmp instead of current directory? depending on how you're deploying, current directory could be a readonly share.ahmelsayed
Can you share how to view the /tmp folder path and how we can see the contents of the /tmp folder? We're not sure the file was properly downloaded. We don't see the folder in MS Azure Storage Explore or the Functions App workspace either . Thx!Prashant Marathay
@Prashant Marathay, any update on this issue? Could you download file now, or did you still have some problem?George Chen

1 Answers

0
votes

Now Python Azure function doesn't allow to write the file, it's read-only mode and this is not able to change. So you could not use get_blob_to_path cause you could write file to your disk.

So if you just want to read the text file content, you could use the below code

filename = "test.txt"
account_name = "storage account"
account_key = "account key"

input_container_name="test"
block_blob_service = BlockBlobService(account_name, account_key)
blobstring = block_blob_service.get_blob_to_text(input_container_name, filename).content

Also you could use the function blob binding to read the content, bind the inputblob as a stream.

def main(req: func.HttpRequest,inputblob: func.InputStream) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

name = req.params.get('name')
if not name:
    try:
        req_body = req.get_json()
    except ValueError:
        pass
    else:
        name = req_body.get('name')

if name:
    return func.HttpResponse(inputblob.read(size=-1))
else:
    return func.HttpResponse(
         "Please pass a name on the query string or in the request body",
         status_code=400
    )

enter image description here