Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stream data to azure block blob in python

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()
like image 670
Greg Clinton Avatar asked Sep 15 '17 16:09

Greg Clinton


People also ask

How do I send data to azure Blob Storage?

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.


1 Answers

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()

enter image description here

You could refer to this thread Azure storage: Uploaded files with size zero bytes.

like image 72
Jay Gong Avatar answered Sep 29 '22 18:09

Jay Gong