我想用python将数据流传输到azure块blob。下面的代码创建blob但最终为零字节。我怎样才能做到这一点?
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()
答案 0 :(得分:1)
您似乎错过了关键代码:
stream.seek(0)
我将流Position property
设置为0,然后您的代码就可以了。
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()
答案 1 :(得分:0)
我建议使用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')