'Read files to Azure function

I have some files stored in Azure Storage ,and I want to iterate and read them from azure function (in python) How should I do that?



Solution 1:[1]

You can use below code to loop through all the containers and directories present in the Storage account.

import os
from azure.storage.blob import BlockBlobService

ACCOUNT_NAME = "<ACCOUNT_NAME>"
SAS_TOKEN='<SAS TOKEN>'

blob_service = BlockBlobService(account_name=ACCOUNT_NAME,account_key=None,sas_token=SAS_TOKEN)

print("\nList blobs in the container")
containers = blob_service.list_containers() #lists the containers
for cntnr in containers:
    generator = blob_service.list_blobs(cntnr.name) #lists the blobs inside containers
    for blob in generator:
        print("\t Blob name: "+cntnr.name+'/'+  blob.name)
        file=blob_service.get_blob_to_text(cntnr.name,blob.name) #lets you read content directly
        file=blob_service.get_blob_to_path(cntnr.name,blob.name,blob.name) #lets you download the content to local and from there you can read the file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SwethaKandikonda-MT