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.

Sudarshan
  • 702
  • 6
  • 24

1 Answers1

2

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

SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18
  • 1
    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). – Sudarshan Sep 08 '22 at 08:04