How to read the data from multiple blob files using python?

Divya Sahu 21 Reputation points
2023-06-08T18:34:49.5933333+00:00

I'm able to get the list of blobs. When reading the data from all the files using Bytes IO it works for 1st blob file but for rest other blob files it throws error that resource not found.

with BytesIO() as input_blob:

blob_client = container_client.get_blob_client(path.name)
                        
blob_client.download_blob().download_to_stream(input_blob)

input_blob.seek(0)
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
{count} votes

1 answer

Sort by: Most helpful
  1. KarishmaTiwari-MSFT 20,772 Reputation points Microsoft Employee Moderator
    2023-06-13T01:57:09.2566667+00:00

    @Divya Sahu Thanks for posting your query on Microsoft Q&A.

    You can achieve this by listing all blobs in a container and downloading them one by one.
    Try the following sample code in Python and modify as per your requirement as needed.

    from azure.storage.blob import BlobServiceClient
    import io
    
    
    def download_blob_to_stream(blob_service_client: BlobServiceClient, container_name, blob_name):
        blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
    
     
    
        # readinto() downloads the blob contents to a stream and returns the number of bytes read
        stream = io.BytesIO()
        num_bytes = blob_client.download_blob().readinto(stream)
        print(f"Number of bytes: {num_bytes}")
    
     
    
    connection_string = "<connection_string>"
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    
     
    
    container_name = "<container_name>"
    blobs = blob_service_client.get_container_client(container_name).list_blobs()
    
    for blob in blobs:
        download_blob_to_stream(blob_service_client, container_name, blob)
    

    If you have any questions at all, please let me know in the "comments" and I would be happy to help you. Comment is the fastest way of notifying the experts.

    Please don’t forget to Accept Answer and hit Yes for "was this answer helpful" wherever the information provided helps you. This can be beneficial to other community members for remediation for similar issues.

    User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.