Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 referring to this, this but they don't use the "Blob SAS token" or "Blob SAS URL"

Can someone help with a python code that enables to read the data as a data frame?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
249 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...