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()
Upload contents of a folder to Data Box Blob storage To get your account key, in the Azure portal, go to your storage account. Go to Settings > Access keys, select a key, and paste it into the AzCopy command. If the specified destination container does not exist, AzCopy creates it and uploads the file into it.
It seems that you've missed the key line of code:
stream.seek(0)
I set the stream's Position property
to 0 then your code works.
import io
import struct
from azure.storage.blob import BlockBlobService
storage = BlockBlobService('acct-xxx', 'key-xxx')
stream = io.BytesIO()
stream.write(struct.pack("d", 12.34))
stream.write(struct.pack("d", 56.78))
stream.seek(0)
storage.create_blob_from_stream("mycontainer", "myblob", stream)
stream.close()
You could refer to this thread Azure storage: Uploaded files with size zero bytes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With