【问题标题】:Azure Storage Account blob stream with Python使用 Python 的 Azure 存储帐户 blob 流
【发布时间】:2020-08-24 10:16:03
【问题描述】:

使用最新的 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 上有另一种更适合这种需求的服务。

【问题讨论】:

  • 您的意思是让 blob 流式传输吗?如果是这样,此代码将有所帮助:with io.BytesIO() as input_io: blob_service.get_blob_to_stream(container_name=container_name, blob_name=blob.name, stream=input_io)
  • get_blob_to_stream 接缝在 azure.storage.blob.baseblobservice 的旧版本中可用,该版本不再可用。也许我错过了什么!

标签: python hdf5 azure-storage-account


【解决方案1】:

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)

【讨论】:

  • 如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…
  • 在 Windows 和 mac 上尝试并确保安装了 azure-storage-blob 包的 12.4.0 版本后,在两个操作系统上都找不到 azure.storage.blob.baseblobservice安装了最新版本的python。
  • from azure.storage.blob.baseblobservice import BaseBlobService 由 azure-storage 0.30.0 提供,不再可用。 12.4.0 包有很多可能性可以打开文件流,但在所有情况下,它们会将整个文件下载到内存中,而不是仅仅读取给打开的流/
  • @FredericThibault azure-storage 0.37.0(最新的但我安装失败)仍然可用。我尝试了 azure-storage 0.36.0,它成功了。看来没有比get_blob_to_stream更好的办法了。
  • 感谢您的帮助。我能够使其适用于 0.36.0 版本。但不幸的是 get_blob_to_stream 将整个文件下载到内存中。我不知道 Azure 上是否存在一项服务,可以从应用程序服务工作并打开一个文件,例如它是否是本地文件,并且只有一个文件流可以读取、查找……而无需将所有文件加载到内存中。
猜你喜欢
  • 2022-11-11
  • 2014-07-25
  • 2019-03-08
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 2021-11-20
  • 1970-01-01
相关资源
最近更新 更多