otk
otk

Reputation: 411

What replaces the old BlockBlobService.get_blob_to_bytes in the new BlobServiceClient?

I have an old azure function which uses BlockBlobService ->get_blob_to_bytes. As described here: https://github.com/uglide/azure-content/blob/master/articles/storage/storage-python-how-to-use-blob-storage.md#download-blobs

I belive i need to update BlockBlobService to BlobServiceClient. But i can not find the get_blob_to_bytes in the BlobServiceClient?

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

How can i get blob to bytes using the BlobServiceClient?

Edit; or do i need to use: 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

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

i got

Name: azure-storage-blob
Version: 12.16.0

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

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.

Anyone?

Upvotes: 0

Views: 778

Answers (1)

SwethaKandikonda
SwethaKandikonda

Reputation: 8254

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

Yes, like you have mentioned there is no get_blob_to_bytes() in BlobClient. However, you can get the desired results using 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)

Results:

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.

However, since your requirement is to get the bytes from queue use QueueClient. Below is something you can try to achieve your requirement.

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

Results:

enter image description here

Upvotes: 2

Related Questions