从python中的azure blob存储读取文件

时间:2021-04-15 13:23:16

标签: python azure azure-storage-blobs

我在读取 Blob 存储中的文件时遇到问题。 我的文件上只有文字。

例如我想从我的文件中获取信息 我的档案

{......}

蚂蚁我想进入这样的变量 s = {......}

我像这样上传到 blob 存储字符串中。

    blob = BlobClient.from_connection_string(conn_str="DefaultEndpointsProtocol=https;AccountName=dasdasdas;AccountKey=sdf+sdfds+dfds==;EndpointSuffix=core.windows.net", container_name="XXXXXX", blob_name="XXXX.json")
    
    
    store_items = 'swx'
    data = str(store_items) + str(conversation_reference)
    blob.upload_blob(data, overwrite=True)

现在我想得到什么价值并使用它。

我尝试这样的事情

block_blob_service = BlockBlobService(account_name='XXXXX', account_key='XXXXX+XXXXX+XXXXX==')

blob2 = block_blob_service.get_blob_to_text('XXXXX', 'XXXXX.json')
print (blob2.content)

但它不起作用,它似乎是旧代码。 错误

NameError: name 'BlockBlobService' is not defined

它需要 <= 2.10 版本。我使用的是 4.10,我无法使用 2.10,因为我的程序无法运行。

知道如何解决吗?

1 个答案:

答案 0 :(得分:1)

也许您可以尝试使用此代码:

from azure.storage.blob import BlobServiceClient
connection_string=''
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client("<container name>")
blob_client = container_client.get_blob_client("<blob name>")
blobstr = blob_client.download_blob().readall().decode("utf-8") # read blob content as string

请参考Quickstart: Manage blobs with Python v12 SDK