How to stream data to azure block blob in python

Viewed 5655

I want to stream data to azure block blob with python. The code below creates the blob but ends up with zero bytes. How can I make this work?

import io
import struct
from azure.storage.blob import BlockBlobService

storage = BlockBlobService('acct-xxx', 'key-xxx')
stream = io.BytesIO()
storage.create_blob_from_stream("mycontainer", "myblob", stream)
stream.write(struct.pack("d", 12.34))
stream.write(struct.pack("d", 56.78))
stream.close()
2 Answers

I recommend using smart_open.

from smart_open import open

# stream from Azure Blob Storage
with open('azure://my_container/my_file.txt') as fin:
    for line in fin:
        print(line)

# stream content *into* Azure Blob Storage (write mode):
with open('azure://my_container/my_file.txt', 'wb') as fout:
    fout.write(b'hello world')
Related