How to read pickle file from Azure Blob without downloading?

Viewed 368

So far, I am able to load it but I have to download it and it takes too much time. I want to read the pickle file directly.
Here's the code

def get_vector_blob(blob_name):
    connection_string = <connection string>
    container_name = <container name>
    blob_client = BlobClient.from_connection_string(connection_string, container_name, blob_name)
    downloader = blob_client.download_blob(0)
    
    # Load to pickle
    b = downloader.readall()
    vector = pickle.loads(b)
    
    return vector
1 Answers

Try readinto(stream) or get_blob_to_stream from azure.storage.blob.baseblobservice

from azure.storage.blob.baseblobservice import BaseBlobService
import io
connection_string = ""
container_name = ""
blob_name = ""
blob_service = BaseBlobService(connection_string=connection_string)
with io.BytesIO() as input_io:
blob_service.get_blob_to_stream(container_name=container_name, blob_name=blob_name, stream=input_io)

References: Access azure blob storage files with python without downloading - Microsoft Q&A and hdf5 - Azure Storage Account blob stream with Python - Stack Overflow

Related