问题描述
使用最新的azure.storage.blob(12.4.0)python库,我需要在blob中打开流,而无需将其完全下载到内存中。
我将hdf5文件存储在存储帐户中,使用h5py(2.10.0),我需要提取一些信息,读取数据而不将文件加载到内存中。这些文件可以包含许多千兆字节的数据。
container_client = blob_service_client.get_container_client('sample')
blob = container_client.get_blob_client('SampleHdF5.hdf5')
stream = BytesIO()
downloader = blob.download_blob()
# download the entire file in memory here
# file can be many giga bytes! Big problem
downloader.readinto(stream)
# works fine to open the stream and read data
f = h5py.File(stream,'r')
也许Azure上还有另一种更适合这种需求的服务。
解决方法
get_blob_to_stream
可以与azure.storage.blob.baseblobservice
一起引用here使用。有我用过的包裹。
from azure.storage.blob.baseblobservice import BaseBlobService
import io
connection_string = ""
container_name = ""
blob_name = ""
blob_service = BaseBlobService(connection_string=connection_string)
with io.BytesIO() as input_io:
blob_service.get_blob_to_stream(container_name=container_name,blob_name=blob_name,stream=input_io)