0

I am trying to download data from an Azure Storage Account using azure-storage-blob's BlobServiceClient class. When I run this snippet from a Jupyter notebook, I manage to retrieve the data without any problem:

from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(CONNECTION_STRING)
blob_client = blob_service_client.get_blob_client(path, 'processed.csv')

CONNECTION_STRING is the connection string to my Azure Storage Account and path, the path to the processed.csv data onto this Storage Account.

However, when running the same script from within a fastAPI application deployed onto Azure App Services, I cannot manage to instantiate BlobServiceClient. Under the Jupyter notebook, print(blob_service_client) returns a BlobServiceClient object, under Azure App Services it returns null.

I am using azure-storage-blob==12.13.1, which is specified in my requirements.txt.

Sheldon
  • 4,084
  • 3
  • 20
  • 41
  • Where does CONNECTION_STRING come from? An environment variable? Hard coded? – JarroVGIT Oct 20 '22 at 06:08
  • Thanks for your reply. CONNECTION_STRING is an environment variable. I was able to print it in the swagger so I confirm that it is being read properly. – Sheldon Oct 20 '22 at 11:53

1 Answers1

1

I have used below sample code to get the file data from blob.

app = FastAPI()

@app.get("/")
def  read_root():
return {"Hello": "World"}

@app.get("/items")
def  download_blob():
    try:
        name = "<Filename>"
        container_name = "<Container Name."
        connect_str = "<Connection String>"
        blob_service_client = BlobServiceClient.from_connection_string(connect_str)
        blob_client = blob_service_client.get_blob_client(container=container_name, blob=name)
        print(blob_client.download_blob().readall())
        return {"File downloaded":blob_client.download_blob().readall()}

    except  Exception  as e:
        print(e.message)
        return e.message

Result before deployed in azure

enter image description here

After Deployed in Azure

Deployed through VS Code. Make sure all your required packages are deployed in azure.

enter image description here

Web App response.

enter image description here

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15
  • Thanks for your detailed reply @Delliganesh Sevanesan. I will try modifying your sample code to meet my needs. – Sheldon Oct 31 '22 at 18:32
  • It turns out that 1) my `path` was not created correctly 2) `CONNECTION_STRING` was not read properly from the Azure App Settings using `os.environ.get("AZURE_CONNECTION_STRING") `, contrarily to what I initially thought. – Sheldon Nov 01 '22 at 21:49
  • Although it did not directly fix my problem, your code sample is a nice MRE, so I am accepting your answer and upvoting it. Thanks again for your help! – Sheldon Nov 01 '22 at 21:55