0

I'm currently using the Azure Blob Storage SDK for Python. For my project I want to read/load the data from a specific blob without having to download it / store it on disk before accessing.

According to the documentation loading a specfic blob works for my with:

blob_client = BlobClient(blob_service_client.url,
 container_name,
 blob_name,
 credential)
data_stream = blob_client.download_blob()
data = data_stream.readall()

The last readall() command returns me the byte information of the blob content (in my case a image).

With:

with open(loca_path, "wb") as local_file:
 data_stream.readinto(my_blob)

it is possible to save the blob content on disk (classic downloading operation)

BUT: Is it also possible to convert the byte data from data = data_stream.readall() directly into an image?

It already tried image_data = Image.frombytes(mode="RGB", data=data, size=(1080, 1920)) but it returns me an error not enough image data

asked Jul 5, 2022 at 12:37
1

1 Answer 1

0

Here is the sample code for reading the text without downloading the file.

from azure.storage.blob import BlockBlobService, PublicAccess
accountname="xxxx"
accountkey="xxxx"
blob_service_client = BlockBlobService(account_name=accountname,account_key=accountkey)
container_name="test2"
blob_name="a5.txt"
#get the length of the blob file, you can use it if you need a loop in your code to read a blob file.
blob_property = blob_service_client.get_blob_properties(container_name,blob_name)
print("the length of the blob is: " + str(blob_property.properties.content_length) + " bytes")
print("**********")
#get the first 10 bytes data
b1 = blob_service_client.get_blob_to_text(container_name,blob_name,start_range=0,end_range=10)
#you can use the method below to read stream
#blob_service_client.get_blob_to_stream(container_name,blob_name,start_range=0,end_range=10)
print(b1.content)
print("*******")
#get the next range of data
b2=blob_service_client.get_blob_to_text(container_name,blob_name,start_range=10,end_range=50)
print(b2.content)
print("********")
#get the next range of data
b3=blob_service_client.get_blob_to_text(container_name,blob_name,start_range=50,end_range=200)
print(b3.content)

For complete information you can check the document with Python libraries.

answered Aug 3, 2022 at 2:02
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.