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

Alex
  • 11
  • 4
  • Does this answer your question? [Azure Blobstore: How can I read a file without having to download the whole thing first?](https://stackoverflow.com/questions/62733213/azure-blobstore-how-can-i-read-a-file-without-having-to-download-the-whole-thin) – Ecstasy Jul 06 '22 at 04:49

1 Answers1

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.

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15