尝试从 Azure Blob 存储读取时出现 FileNotFound 错误

问题描述 投票:0回答:1
我正在构建一个 Streamlit 应用程序,该应用程序从 Azure Blob 存储读取文件(在本例中为 .npy 文件)。我通过在定义为

read_blob_content

 的函数中指定帐户名称、帐户密钥、容器名称和 Blob 名称(文件名)来访问存储帐户。但由于某种原因,我在运行应用程序时收到此错误:

FileNotFoundError: [Errno 2] No such file or directory: 'file.npy' Traceback: File "C:\Users...\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 535, in _run_script exec(code, module.__dict__) File "C:\Users...\app\main.py", line 34, in <module> v3_embedding = np.load(read_blob_content(account_name, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users...\Lib\site-packages\numpy\lib\npyio.py", line 427, in load fid = stack.enter_context(open(os_fspath(file), "rb")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^
对我来说,这似乎表明我在某种程度上输入了错误的路径,即使文件名与我的 Azure Blob 存储帐户上的名称匹配。有人知道我做错了什么吗?

这是附加上下文的代码:

在main.py中

v3_embedding = np.load(read_blob_content(account_name, account_key, container_name, blob_name))

read_blob_content

功能:

def read_blob_content(storage_account_name, storage_account_key, container_name, blob_name): '''Reads files stored on Azure blob storage container''' # Create a BlobServiceClient blob_service_client = BlobServiceClient(account_url=f"https://{storage_account_name}.blob.core.windows.net", credential=storage_account_key) # Get the blob content as a stream blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name) # Get the blob content as a stream blob_stream = blob_client.download_blob() # Read the content from the stream blob_content = blob_stream.readall().decode('utf-8') return blob_content
    
python azure azure-blob-storage streamlit file-not-found
1个回答
0
投票

我尝试使用以下代码使用 Streamlit 应用程序从 Azure Blob 存储读取 .npy blob 数据:

代码:

import streamlit as st import numpy as np from read_blob import read_blob_content from io import BytesIO def main(): st.title("Streamlit App") account_name = "<storage_key>" account_key = "<storage_key>" container_name = "<container_name>" blob_name = "<blobname.npy>" try: blob_content = read_blob_content(account_name, account_key, container_name, blob_name) with BytesIO(blob_content) as f: v3_embedding = np.load(f, allow_pickle=True) st.write("Loaded .npy file from Azure Blob Storage:") st.write(v3_embedding) except FileNotFoundError: st.error("The specified file was not found.") except Exception as e: st.error(f"An error occurred while loading the data: {str(e)}") if __name__ == "__main__": main()

read_blob.py:

from azure.storage.blob import BlobServiceClient def read_blob_content(storage_account_name, storage_account_key, container_name, blob_name): '''Reads files stored on Azure blob storage container''' blob_service_client = BlobServiceClient(account_url=f"https://{storage_account_name}.blob.core.windows.net", credential=storage_account_key) blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name) blob_data = blob_client.download_blob() blob_content = blob_data.readall() return blob_content

输出:

Streamlit应用代码运行成功,如下图:

C:\Users\xxxxxxxx\Documents\xxxxxxxxx\app>streamlit run main.py You can now view your Streamlit app in your browser. Local URL: http://localhost:8501 Network URL: http://192.168.0.126:8501

enter image description here

Streamlit 应用程序在浏览器中读取 .npy blob 文件数据,如下所示。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.