我尝试了以下链接中的示例:Write Python DataFrame as CSV into Azure Blob但是Azure.storage.blob中不再提供BlockBlobService,因此我尝试使用BlockBlobClient,并可以使用以下代码创建,删除容器。但是,我找不到一种在容器中创建blob并将其记录从数据框中写入记录的方法,如上面的链接所述。请帮助我创建一个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")