gpt4 book ai didi

python - 新 BlobServiceClient 中的旧 BlockBlobService.get_blob_to_bytes 被什么替代?

转载 作者:行者123 更新时间:2023-12-03 05:12:32 25 4
gpt4 key购买 nike

我有一个旧的 Azure 函数,它使用 BlockBlobService ->get_blob_to_bytes。如此处所述:https://github.com/uglide/azure-content/blob/master/articles/storage/storage-python-how-to-use-blob-storage.md#download-blobs

我相信我需要将 BlockBlobService 更新为 BlobServiceClient。但我在 BlobServiceClient 中找不到 get_blob_to_bytes?

Exception: AttributeError: 'BlobServiceClient' object has no attribute 'get_blob_to_bytes'

如何使用 BlobServiceClient 将 blob 转换为字节?

编辑;或者我需要使用:https://learn.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.baseblobservice.baseblobservice?view=azure-python-previous#azure-storage-blob-baseblobservice-baseblobservice-get-blob-to-bytes

编辑2; https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/storage/azure-storage-blob/migration_guide.md#storage-blob-service-sdk-migration-guide-from--2x-to-12x

enter image description here

我得到了

Name: azure-storage-blob
Version: 12.16.0

但是没有 BlobClient.get_blob_to_bytes...或者至少当我尝试它时没有?

Edit3:为了进一步增加困惑,我正在使用队列中的消息。实例化 BlobClient 时需要 account_url、container_name 和 blob_name。如果您有 container 那就没问题了,但我有一个 queue .

有人吗?

最佳答案

But there is no BlobClient.get_blob_to_bytes.... Or atleast not when i try it??

是的,就像您提到的,BlobClient 中没有 get_blob_to_bytes()。但是,您可以使用 download_blob().readall() 获得所需的结果。

from  azure.storage.blob  import  BlobClient

connection_string="<CONNECTION_STRING>"

client = BlobClient.from_connection_string(connection_string,'container','abc.txt')

blob_bytes = client.download_blob().readall()
print(blob_bytes)

结果:

enter image description here

<小时/>

Edit3: To further add confusion, i 'm consuming messages from a Queue. And when instantiating a BlobClient is expects account_url, container_name and blob_name. Thats all well if you have a container, but i have a queue.

但是,由于您的要求是从队列中获取字节,请使用QueueClient。您可以尝试以下方法来实现您的要求。

from azure.storage.queue import QueueClient
import base64

connection_string="<CONNECTION_STRING>"

queue = QueueClient.from_connection_string(conn_str=connection_string, queue_name='queue')
messages = queue.receive_messages()
for message in messages:
print(base64.b64decode(message.content).decode('utf-8').encode('utf-8'))

结果:

enter image description here

关于python - 新 BlobServiceClient 中的旧 BlockBlobService.get_blob_to_bytes 被什么替代?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76014556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com