簡體   English   中英

使用 Python 將 Blob 下載到本地存儲

[英]Download Blob To Local Storage using Python

我正在嘗試下載 blob 文件並將其本地存儲在我的機器上。 文件格式是 HDF5 (我迄今為止有限/沒有經驗的格式)。

到目前為止,我已經成功地使用下面的腳本下載了一些東西 關鍵問題是它似乎不是完整的文件。 直接從存儲資源管理器下載文件時,大約為 4,000kb。 我保存的 HDF5 文件是 2kb。

我究竟做錯了什么? 我在某處錯過了 readall() 嗎?

我第一次使用 blob 存儲和 HDF5,所以現在有點卡住了。 由於 azure.storage.blob 模塊已更新,許多舊問題似乎都在使用已棄用的命令。

from azure.storage.blob import BlobServiceClient
from io import StringIO, BytesIO
import h5py

# Initialise client
blob_service_client = BlobServiceClient.from_connection_string("my_conn_str")
# Initialise container
blob_container_client = blob_service_client.get_container_client("container_name")
# Get blob
blob_client = blob_container_client.get_blob_client("file_path")

# Download
download_stream = blob_client.download_blob()

# Create empty stream
stream = BytesIO()
# Read downloaded blob into stream
download_stream.readinto(stream)
# Create new empty hdf5 file
hf = h5py.File('data.hdf5', 'w')
# Write stream into empty HDF5
hf.create_dataset('dataset_1',stream)
# Close Blob (& save)
hf.close()

我試圖在我的系統中重現與您嘗試的代碼相同的問題的場景

所以我嘗試了另一種解決方案,將 hdf5 文件讀取為 stream 並將其寫入另一個 hdf5 文件

嘗試使用此解決方案。出於測試目的獲取一些虛擬數據。

from azure.storage.blob import BlobServiceClient
from io import StringIO, BytesIO
import numpy as np
import h5py

# Initialise client
blob_service_client = BlobServiceClient.from_connection_string("Connection String")
# Initialise container
blob_container_client = blob_service_client.get_container_client("test//Container name")
# Get blob
blob_client = blob_container_client.get_blob_client("test.hdf5 //Blob name")

print("downloaded the blob ")
# Download
download_stream = blob_client.download_blob()
stream = BytesIO()
downloader = blob_client.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')


//dummy data
data_matrix = np.random.uniform(-1, 1, size=(10, 3))

with h5py.File(stream, "r") as f:
    # List all groups
    print("Keys: %s" % f.keys())
    a_group_key = list(f.keys())[0]

    # Get the data
    data = list(f[a_group_key])
    data_matrix=data
    print(data)

with h5py.File("file1.hdf5", "w") as data_file:
    data_file.create_dataset("group_name", data=data_matrix)

OUTPUT

在此處輸入圖像描述

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM