读取Azure Blob Storage时出现FileNotFound错误

0 投票
1 回答
89 浏览
提问于 2025-04-14 17:11

我正在制作一个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

1 个回答

1

我尝试了以下代码,通过一个Streamlit应用从Azure Blob存储中读取.npy格式的数据。

代码:

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

在这里输入图片描述

Streamlit应用在浏览器中成功读取了.npy格式的文件数据,显示如下。

在这里输入图片描述

撰写回答