title: How can I download an image from Azure Blob Storage and store it into a local directory?
body: <p>So here is the general situation. I have a directory inside an Azure blob storage container called <code>dir</code>. There are multiple images within this directory. Let's say they are <code>img1.png</code>, <code>img2.png</code>, and so on. I want to take these images from the blob storage and store them in a local repository called <code>localDir</code>.</p>
  <p>I want to be able to do this in an iterative fashion where I loop through each image inside <code>dir</code> and store it in <code>localDir</code>. That way, I can easily skip over some images. I also need to be able to do this with the latest version of <code>azure-storage-blob</code>, which would be version 12.8.0, if I'm not mistaken.</p>
  <p>Assuming that I have the <code>container_name</code> and <code>connect_str</code>. Here is the code so far,</p>
  <pre class="lang-py prettyprint-override"><code>from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
  
  blob_service_client = BlobServiceClient.from_connection_string(connect_str)
  container_client = blob_service_client.get_container_client(container_name)
  
  img_blob_list = container_client.list_blobs(name_starts_with='dir/')
  
  for blob in img_blob_list:
      img_blob_client = container_client.get_blob_client(blob=blob['name'])
  </code></pre>
  <p>With this code I can obtain the <code>img_blob_client</code> for each image in the Azure <code>dir</code> blob. However, I do not understand how to take this image and copy it into the local directory <code>localDir</code>. I have tried to do the following:</p>
  <pre class="lang-py prettyprint-override"><code>for blob in img_blob_list:
      img_blob_client = container_client.get_blob_client(blob=blob['name'])
      with open(f&quot;./localDir/{blob['name']}&quot;, &quot;wb&quot;) as dst:
          dst.write(img_blob_client.download_blob().readall())
  </code></pre>
  <p>After running this code, the files appeared in the desired destination folder as expected. However, they wouldn't open or render at all.</p>
  <p>Any help would be much appreciated.</p>