4

I have some files in my azure-storage account. i need to download them using get_blob_to_stream.it is returning azure.storage.blob.models.Blob object. so i couldn't download it by using below code.

def download(request):
    file_name=request.POST['tmtype']
    fp = open(file_name, 'wb')
    generator = block_blob_service.list_blobs(container_name)
    for blob in generator:
        print(blob.name)
        if blob.name==file_name:             
            blob=block_blob_service.get_blob_to_stream(container_name, blob.name, fp,max_connections= 2)    
        response = HttpResponse(blob, content_type="image/png")
        response['Content-Disposition'] = "attachment; filename="+file_name
        return response

2 Answers2

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.

Pablo
  • 10,425
  • 1
  • 44
  • 67
Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
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

  • How would I use `get_blob_to_stream` in an Azure Function with BlobTrigger? Or is `func.InputStream` the equivalent? – ericOnline Sep 18 '20 at 15:23