簡體   English   中英

Python-從Azure筆記本中的C驅動器訪問文件

[英]Python - accessing files from C drive in Azure notebook

我在C盤中有h5文件。 我無法在Azure的h5中上傳數據集,因為它只有468 MB。 我如何從代碼本身讀取它。 在沒有Azure的情況下,將jupyter筆記本放在本地計算機上,我可以使用以下coede進行訪問:

使用h5py.File('SVHN_single_grey1.h5','r')作為hdf:

這在Azure中不起作用,因為它無法訪問計算機上的本地文件。

如果可以通過URL直接從Internet訪問H5文件,則可以嘗試使用下面的代碼在Azure Notebook中讀取它。

import requests
from io import BytesIO
import h5py

r = requests.get("<an url for accessing your H5 file, such as https://host:port/.../SVHN_single_grey1.h5>")
f = BytesIO(r.content)
with h5py.File(f) as hdf:
    ...

如果不是,則必須先將H5文件作為資源URL發布到Internet服務,然后通過上面的代碼使用它。 我建議使用Azure官方工具azcopy幫助將其作為blob上傳到Azure Blob存儲,請參考官方教程Tutorial: Migrate on-premises data to cloud storage by using AzCopyTutorial: Migrate on-premises data to cloud storage by using AzCopy以了解更多詳細信息。 然后,您可以按照下面的示例代碼再次閱讀。

from azure.storage.blob.baseblobservice import BaseBlobService
from azure.storage.blob import BlobPermissions
from datetime import datetime, timedelta
import requests
from io import BytesIO
import h5py

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
)

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)
f = BytesIO(r.content)
with h5py.File(f) as hdf:
    ...

另外,這里是其他示例代碼也可以正常工作。

from azure.storage.blob.baseblobservice import BaseBlobService
from io import BytesIO
import h5py

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
)

stream = BytesIO()
blob_service.get_blob_to_stream(container_name, blob_name, stream)
with h5py.File(stream) as hdf:
    ...

暫無
暫無

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

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