[Django]-How to download data from azure-storage using get_blob_to_stream

0👍

You can actually use the get_blob_to_path property, below is an example in python:

from azure.storage.blob import BlockBlobService

bb = BlockBlobService(account_name='', account_key='')
container_name = ""
blob_name_to_download = "test.txt"
file_path ="/home/Adam/Downloaded_test.txt"

bb.get_blob_to_path(container_name, blob_name_to_download, file_path, open_mode='wb', snapshot=None, start_range=None, end_range=None, validate_content=False, progress_callback=None, max_connections=2, lease_id=None, if_modified_since=None, if_unmodified_since=None, if_match=None, if_none_match=None, timeout=None)

This example with download a blob file named: “test.txt”, in a container, to File_path”/home/Adam/Downloaded_test.txt” , you can also keep the same name if you’d like to as well. You can find more samples including this one in https://github.com/adamsmith0016/Azure-storage

0👍

If you want to use get_blob_to_stream. You can download with below code:

with io.open(file_path, 'wb') as file:
    blob = block_blob_service.get_blob_to_stream(
           container_name=container_name,
           blob_name=blob_name, stream=file, 
           max_connections=2)

Just note that the file content will be streamed to the file rather than the returned blob object. The blob.content should be None. That is by design. See https://github.com/Azure/azure-storage-python/issues/538.

Leave a comment