1

I have a azure function created in Python 3.9 and I will be executing the function using Http Trigger via Azure data factory.

Now, in my python azure function, I want to access storage container from my storage account and read the files from the same container, in order to perform few data manipulations on the file data.

How can I achieve this ?

I found one similar kind of question here : Read data from Azure blob storage in Azure Function in python

But, in the solution of this question author have explicitly accessed a specific file but in my case I want to access all the files of container one by one and read their data.

This would work just like accessing the files from a directory of local machine and one by one reading contents of each accessed files.

3
  • You would need to list all blobs first and then loop over the blobs list and read them one by one. Commented Sep 1, 2022 at 14:18
  • ok. But, how will I fetch the list ? Commented Sep 2, 2022 at 8:44
  • Can I fetch list of blobs using storage container ? Commented Sep 2, 2022 at 8:45

1 Answer 1

3

After reproducing from my end, I could be able to achieve your requirement using BlockBlobService. Below is the code that worked for me.

import logging

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

ACCOUNT_NAME = "<ACCOUNT_NAME>"
SAS_TOKEN='<SAS_TOKEN>'

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    
    file="" 
    fileContent=""       
    blob_service = BlockBlobService(account_name=ACCOUNT_NAME,account_key=None,sas_token=SAS_TOKEN)
    containername="<CONTAINER_NAME>"
    generator = blob_service.list_blobs(container_name=containername) #lists the blobs inside containers
    for blob in generator:
        file=blob_service.get_blob_to_text(containername,blob.name) 
        logging.info(file.content)
        fileContent+=blob.name+'\n'+file.content+'\n\n'

    return func.HttpResponse(f"{fileContent}")

RESULTS:

In my Storage Account

enter image description here

Run Results

enter image description here

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

1 Comment

Thanks for the answer. I have also found a workaround by storing my data into azure-file-share instead of azure-storage-container. Although, this is a great answer (specially screenshots).

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.