使用 azure.storage.blob 将 Python DataFrame 作为 CSV 写入 Azure Blob

Using azure.storage.blob to write Python DataFrame as CSV into Azure Blob

我尝试了以下 link 中的示例: 但是 BlockBlobService 在 azure.storage.blob 中不再可用,所以我尝试使用 BlockBlobClient 并且我可以使用以下代码。但是,我找不到在容器中创建 blob 并将数据框中的记录写入其中的方法,如上文所述 link。请帮忙我想创建一个 blob 并将数据框中的记录写入其中。

from azure.storage.blob import BlobServiceClient

blobService = BlobServiceClient.from_connection_string("**")
#blobService = BlobServiceClient(account_name=accountName, account_key=accountKey)
try:
   new_container = blobService.create_container("containerfromblobservice")
   properties = new_container.get_container_properties()
except ResourceExistsError:
   print("Container already exists.")

问题请参考以下代码

# create data
head = ["col1" , "col2" , "col3"]
value = [[1 , 2 , 3],[4,5,6] , [8 , 7 , 9]]
df = pd.DataFrame (value, columns = head)
output = df.to_csv (index=False, encoding = "utf-8")
print(output)

connection_string=''
# Instantiate a new BlobServiceClient using a connection string
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
# Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client('mycsv')
try:
   # Create new Container in the service
   container_client.create_container()
   properties = container_client.get_container_properties()
except ResourceExistsError:
   print("Container already exists.")

# Instantiate a new BlobClient
blob_client = container_client.get_blob_client("output.csv")
# upload data
blob_client.upload_blob(output, blob_type="BlockBlob")

详情请参考here and here