pythonazureazure-blob-storagestreamlitfile-not-found

FileNotFound Error when trying to read from Azure Blob Storage


I'm building a Streamlit app that reads a file (a .npy file in this case) from Azure Blob Storage. I'm accessing the storage account by specifying the account name, account key, container name, and blob name (file name) in a function I've defined as read_blob_content. For some reason however I get this error when running the app:

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"))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^

To me, this seems to indicate that I am somehow entering the path wrong even though the file name matches what is on my Azure Blob Storage account. Does anybody know what I am doing wrong?

Here is the code for additional context:

In main.py

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

The read_blob_content function:

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

Solution

  • I tried the following code to read a .npy blob data from Azure Blob Storage using a Streamlit app:

    Code :

    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
    

    Output :

    The Streamlit app code ran successfully, as shown below:

    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

    The Streamlit app read the .npy blob file data in the browser, as displayed below.

    enter image description here