3 回答

TA貢獻1936條經驗 獲得超7個贊
我嘗試使用幾種方法來實現您的需求。
這是我的示例代碼。
from azure.storage.blob.baseblobservice import BaseBlobService
import numpy as np
account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<your container name>'
blob_name = '<your blob name>'
blob_service = BaseBlobService(
account_name=account_name,
account_key=account_key
)
示例 1. 使用 sas 令牌生成 blob url 以通過以下方式獲取內容 requests
from azure.storage.blob import BlobPermissions
from datetime import datetime, timedelta
import requests
sas_token = blob_service.generate_blob_shared_access_signature(container_name, blob_name, permission=BlobPermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1))
print(sas_token)
url_with_sas = blob_service.make_blob_url(container_name, blob_name, sas_token=sas_token)
print(url_with_sas)
r = requests.get(url_with_sas)
dat = np.frombuffer(r.content)
print('from requests', dat)
示例 2. 通過以下方式將 blob 的內容下載到內存中 BytesIO
import io
stream = io.BytesIO()
blob_service.get_blob_to_stream(container_name, blob_name, stream)
dat = np.frombuffer(stream.getbuffer())
print('from BytesIO', dat)
示例 3. 使用numpy.fromfilewithDataSource打開帶有 sas 令牌的 blob url,它實際上會將 blob 文件下載到本地文件系統中。
ds = np.DataSource()
# ds = np.DataSource(None) # use with temporary file
# ds = np.DataSource(path) # use with path like `data/`
f = ds.open(url_with_sas)
dat = np.fromfile(f)
print('from DataSource', dat)
我認為示例 1 和 2 更適合您。

TA貢獻1860條經驗 獲得超9個贊
當涉及到 np.savez 時,上述解決方案通常需要工作。
上傳到存儲:
import io
import numpy as np
stream = io.BytesIO()
arr1 = np.random.rand(20,4)
arr2 = np.random.rand(20,4)
np.savez(stream, A=arr1, B=arr2)
block_blob_service.create_blob_from_bytes(container,
"my/path.npz",
stream.getvalue())
從存儲下載:
from numpy.lib.npyio import NpzFile
stream = io.BytesIO()
block_blob_service.get_blob_to_stream(container, "my/path.npz", stream)
ret = NpzFile(stream, own_fid=True, allow_pickle=True)
print(ret.files)
""" ['A', 'B'] """
print(ret['A'].shape)
""" (20, 4) """

TA貢獻1827條經驗 獲得超8個贊
有點晚了,但如果有人想使用 numpy.load 執行此操作,這里是代碼(Azure SDK v12.8.1):
from azure.storage.blob import BlobServiceClient
import io
import numpy as np
# define your connection parameters
connect_str = ''
container_name = ''
blob_name = ''
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blob_client = blob_service_client.get_blob_client(container=container_name,
blob=blob_name)
# Get StorageStreamDownloader
blob_stream = blob_client.download_blob()
stream = io.BytesIO()
blob_stream.download_to_stream(stream)
stream.seek(0)
# Load form io.BytesIO object
data = np.load(stream, allow_pickle=False)
print(data.shape)
添加回答
舉報