Skip to content Skip to sidebar Skip to footer

Python Read From Azure Storage Account

Having hard time in reading a .csv file that is stored in a storage container. I have these details for the container to access: 'Blob SAS token' and 'Blob SAS URL' I have been ref

Solution 1:

If you look at the documentation HERE, it shows how to use a sas url to create a BlobClient. Once you have this, you can follow the instruction in the LINK you shared.

Your final code will look like this.

from azure.storage.blob import BlobClient
import pandas as pd
from io import StringIO

sas_url = "<your_blob_sas url>"
blob_client = BlobClient.from_blob_url(sas_url)
blob_data = blob_client.download_blob()
df = pd.read_csv(StringIO(blob_data.content_as_text()))
print(df)

To upload a dataframe to a container, you can try the following code

from azure.storage.blob import ContainerClient

sas_url = "https://<acct_name>.blob.core.windows.net/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
container = ContainerClient.from_container_url(sas_url)

output = io.StringIO()
head = ["col1" , "col2" , "col3"]
l = [[1 , 2 , 3],[4,5,6] , [8 , 7 , 9]]
df = pd.DataFrame (l , columns = head)
print(df)
output = df.to_csv (index_label="idx", encoding = "utf-8")

blob_client = container_client.upload_blob(name="myblob", data=output)

Post a Comment for "Python Read From Azure Storage Account"