2017-09-15 68 views
0

我想用python将数据流式传输到azure块blob。下面的代码创建了blob,但以零字节结束。我该如何做这项工作?如何在python中将数据流式传输到azure块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

嗨,Greg。任何进展? –

+0

我不认为python库会满足我的需求。 –

回答

0

看来你已经错过了代码的关键行:

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

enter image description here

您可以参考这个线程Azure storage: Uploaded files with size zero bytes

+0

这将需要将整个流保存在内存中。我希望按照流的方式传输数据,而不会产生大量内存占用。 –

+0

@GregClinton嗨,Greg。 storage.create_blob_from_stream实际上是一个用于rest API的http请求包,它是一种同步阻塞模式,因此您无法操作已上传到azure的流。 –

相关问题