Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

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 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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...