Azure Documentation

Downloading Blobs with Azure PowerShell

This document explains how to download blob data from Azure Blob Storage using Azure PowerShell cmdlets. Blob storage is a cloud-based object storage solution for unstructured data like text or binary data.

Prerequisites

  • Azure PowerShell module installed and configured.
  • An Azure Storage account.
  • A container within the storage account.
  • A blob file within the container to download.

Connecting to Your Azure Account

Before you can manage Azure resources, you need to connect to your Azure account. If you haven't already, you can connect using the following cmdlet:

Connect-AzAccount

This will open a browser window for you to authenticate.

Getting Your Storage Account Context

To interact with a storage account, you need to obtain its context. You can do this by specifying the storage account name and, optionally, the resource group.

# Replace 'yourstorageaccountname' with your actual storage account name
$storageAccountName = "yourstorageaccountname"
$resourceGroupName = "yourresourcegroupname" # Optional, if you know the resource group

# Get the storage account
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName

# Get the storage context
$ctx = $storageAccount.Context

Downloading a Blob

The primary cmdlet for downloading blobs is Get-AzStorageBlobContent. You'll need to specify the container name, the blob name (the path to the file within the container), and the destination path on your local machine.

Example: Downloading a single blob

# Replace with your container name, blob name, and desired local path
$containerName = "mycontainer"
$blobName = "path/to/your/blob.txt"
$destinationPath = "C:\Downloads\blob.txt"

# Download the blob content
Get-AzStorageBlobContent -Container $containerName -Blob $blobName -Destination $destinationPath -Context $ctx

Write-Host "Blob '$blobName' downloaded successfully to '$destinationPath'."

Example: Downloading a blob to a specific directory

If you want to download the blob and preserve its directory structure relative to a local target folder, you can use the -Force parameter if the file already exists and you want to overwrite it.

# Download a blob into a specific local folder
$localDirectory = "C:\Data\Blobs\"
$blobName = "images/profile.jpg"

Get-AzStorageBlobContent -Container "user-data" -Blob $blobName -Destination $localDirectory -Context $ctx -Force

Write-Host "Blob '$blobName' downloaded to '$localDirectory'."

Downloading All Blobs in a Container (with filtering)

You can also download multiple blobs. Use Get-AzStorageBlob to list blobs and then pipe them to Get-AzStorageBlobContent. You can filter by prefix.

$containerName = "myfiles"
$localDirectory = "C:\Backup\Files\"
$blobPrefix = "documents/" # Download only blobs starting with 'documents/'

# Create the local directory if it doesn't exist
if (-not (Test-Path $localDirectory)) {
    New-Item -ItemType Directory -Path $localDirectory | Out-Null
}

# Get all blobs with the specified prefix and download them
Get-AzStorageBlob -Container $containerName -Prefix $blobPrefix -Context $ctx | ForEach-Object {
    $blob = $_
    $localFilePath = Join-Path $localDirectory $blob.Name
    Write-Host "Downloading $($blob.Name) to $localFilePath..."
    Get-AzStorageBlobContent -Blob $blob -Destination $localFilePath -Force -Context $ctx
}

Write-Host "All blobs with prefix '$blobPrefix' downloaded from container '$containerName'."
Note: Large blob downloads can take significant time and network bandwidth. Consider using the AzCopy tool for very large transfers or scenarios requiring more advanced features.

Common Parameters

  • -Container: The name of the container.
  • -Blob: The name of the blob to download.
  • -Destination: The local path to save the blob. Can be a file path or a directory.
  • -Context: The storage account context object.
  • -Force: Overwrites the destination file if it already exists.
  • -Prefix: Filters blobs by a prefix, useful for downloading a subset of files.
Tip: To download a blob and save it with a different name locally, specify the full desired local file path in the -Destination parameter.