2

How can i reads a text blob in Azure without downloading it? I am able to download the file and then read it but, i prefer it to be read without downloading.

print("\nList blobs in the container")
generator = block_blob_service.list_blobs(container_name)                  
for blob1 in generator:
    print("\t Blob name: " + blob.name)

Is there any operation in 'blob1' object, which would allow me to read the text file directly.(like blob1.read or blob1.text or something like this)?

2 Answers2

5

You can use get_blob_to_text method.

block_blob_service = BlockBlobService(account_name='myaccount', account_key='mykey')

blob = block_blob_service.get_blob_to_text('mycontainer', 'myblockblob')
print(blob.content)
Pikamander2
  • 7,332
  • 3
  • 48
  • 69
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
-1

You can read the content of txt file in the blob and assign the content to a variable without downloading the file with BlobServiceClient:

blob_service = BlobServiceClient.from_connection_string(<connection string>)
container_client = blob_service.get_container_client(<container name>)
file_content = container_client.get_blob_client(<blob file name>).download_blob().readall()

You can find instruction of configuring Azure Storage connection string here

  • It seems that by calling `download_blob()` you in fact download the blob, which is not OPs intention. – joba2ca Mar 15 '23 at 10:06