Below are a few common ways to pull an image (or any binary file) down from Azure Blob Storage and place it in a specific local directory.
1. Using Python (Azure SDK v12+)
from azure.storage.blob import BlobServiceClient
from pathlib import Path
import os
# ── 1. Settings ─────────────────────────────────────────────
connect_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING") # or hard-code it
container = "my-container"
blob_name = "photos/cat.png" # path *inside* the container
download_dir = Path(r"C:\Images") # local directory you want
download_dir.mkdir(parents=True, exist_ok=True)
local_file = download_dir / Path(blob_name).name # C:\Images\cat.png
# ── 2. Client ───────────────────────────────────────────────
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blob_client = blob_service_client.get_blob_client(container=container,
blob=blob_name)
# ── 3. Download ─────────────────────────────────────────────
with open(local_file, "wb") as fp:
download_stream = blob_client.download_blob()
fp.write(download_stream.readall())
print(f"Downloaded → {local_file}")
Things to note:
AZURE_STORAGE_CONNECTION_STRING can be copied from the Azure Portal (Storage Account ➜ Access keys).
- If you expect big files, use
readinto(fp) or download_blob(max_concurrency=4).readall() for faster throughput.
2. Using Azure CLI
If you prefer a one-liner (no code), log in with az login and then:
az storage blob download \
--account-name <storage_account> \
--container-name my-container \
--name photos/cat.png \
--file "C:\Images\cat.png" \
--auth-mode key # or 'login' / 'login' + RBAC
Extra switches:
--account-key or --sas-token if you don’t want to use environment variables.
--overwrite true if you want to forcibly replace existing files.
3. Using Node.js
import { BlobServiceClient } from "@azure/storage-blob";
import { writeFileSync } from "fs";
import path from "path";
// 1. Config
const connectionString = process.env.AZURE_STORAGE_CONNECTION_STRING;
const containerName = "my-container";
const blobName = "photos/cat.png";
const downloadDir = "C:\\Images";
// 2. Client
const blobService = BlobServiceClient.fromConnectionString(connectionString);
const blobClient = blobService.getContainerClient(containerName)
.getBlobClient(blobName);
// 3. Download
(async () => {
const downloadBlock = await blobClient.download();
const buffer = await streamToBuffer(downloadBlock.readableStreamBody);
const localFile = path.join(downloadDir, path.basename(blobName));
writeFileSync(localFile, buffer);
console.log(`Saved to ${localFile}`);
})();
async function streamToBuffer(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", d => chunks.push(d));
readableStream.on("end", () => resolve(Buffer.concat(chunks)));
readableStream.on("error", reject);
});
}
4. PowerShell (Az module)
$ctx = New-AzStorageContext -StorageAccountName "mystorage" -UseConnectedAccount
Get-AzStorageBlobContent `
-Container "my-container" `
-Blob "photos/cat.png" `
-Destination "C:\Images\cat.png" `
-Context $ctx
Troubleshooting Tips
• 403 / authorization errors → double-check the connection string, SAS token, or role assignments (Storage Blob Data Reader).
• “The specified blob does not exist” → verify container & blob names (they’re case-sensitive).
• Large downloads timing out → use parallel downloads (max_concurrency) or AzCopy.
And that’s it! Pick the method that fits your stack, point it at the container/blob, and write the bytes to your target directory 🎉