How to download azure storage blobs using powershell

The most common way to browse or download a storage blob is to go directly to the Azure portal. However, when there is a need for bulk downloads or automation, one may require writing custom scripts.

This post covers:

  • Getting a list of blobs in a storage account container
  • Applying filters/criteria to narrow down the list
  • Downloading the blobs in the list in parallel

You can also watch deep explanation on YouTube


1. Install azure PowerShell module.

Command to check Az module is already installed

Get-Module -Name Az -ListAvailable

If Az module is already installed, use below command to update it

Update-Module -Name Az -Force

If Az module is not installed, use below command to install it

Install-Module -Name Az -Repository PSGallery -Force

2. Create a new powershell script file, DownloadBlobs.ps1 and add below code to create DownloadBlobs function.

# Function to download the blobs
function 
DownloadBlobs() {

param(

[Parameter(Mandatory=$true)][Object]$storageContext,
[Parameter(Mandatory=$true)][string]$containerName,
[Parameter(Mandatory=$true)][string]$downloadLocation,
[Parameter(Mandatory=$true)][Int32][ValidateScript({$_ -lt 0})] [int]$lastModifedInDays

)



$throttleLimit = 4



# Get the list of blobs

$blobList = Get-AzStorageBlob -Container $containerName -Context $storageContext

Write-Output "Total blobs in the container: " $blobList.Count



# Select blobs that has been modifed in last $lastModifedInDays days
$filteredBlobs = $blobList | Where-Object { $_.LastModified -gt ((Get-Date).Date).AddDays($days) }

Write-Output "Total blobs matching criteria: " $filteredBlobs.Count



Write-Output "Downloading...."

$filteredBlobs | ForEach-Object -Parallel {
Get-AzStorageBlobContent -Container $using:containerName -Blob $_.Name -Context $using:storageContext -Destination $using:downloadLocation
} -ThrottleLimit $throttleLimit


Write-Output "Download Complete"
}

The function accepts four parameters:

  • storageContext: An object containing the details for connecting to the storage account.
  • containerName: The name of the container where the blobs are stored.
  • downloadLocation: The location on your machine where the blobs will be downloaded.
  • lastModifiedInDays: A filter that allows you to download blobs created within the last lastModifiedInDays from today.

The function first retrieves a list of all blobs in the container and then applies the days filter. You can choose to combine both steps or skip the filter, depending on your needs.

It then downloads the blobs using parallel iterations. The throttleLimit variable controls the number of parallel threads for downloading. This limit should be equal to or less than the number of CPU cores on your machine.


3. Next, we add code to login into azure and call the above function. Update the DownloadBlobs.ps1 file with below code.

### *** This script is to download blobs from an Azure storage account. *** ###

$subscriptionId = "cqewdjbewhdvqdhqdqdh"
$storageAccountName = "devsa"
$storageAccountKey = "q5u823hbdhdbeddvhqwdh"
$containerName = "orders"
$downloadLocation = "/users/shared/"

# Login
Connect-AzAccount

# Set Subscription
Set-AzContext -Subscription $subscriptionId 

# Create storage context
$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

# Download the blobs
DownloadBlobs -storageContext $storageContext  -containerName $containerName -downloadLocation $downloadLocation -lastModifedInDays -2

# Function to download the blobs
function DownloadBlobs() {
    param(
        
        [Parameter(Mandatory=$true)][Object]$storageContext,
        [Parameter(Mandatory=$true)][string]$containerName,
        [Parameter(Mandatory=$true)][string]$downloadLocation,
        [Parameter(Mandatory=$true)][Int32][ValidateScript({$_ -lt 0})] [int]$lastModifedInDays
    )

    $throttleLimit = 4

    # Get the list of blobs
    $blobList = Get-AzStorageBlob -Container $containerName -Context $storageContext
    Write-Output "Total blobs in the container: " $blobList.Count
    
    # Select blobs that has been modifed in last $lastModifedInDays days
    $filteredBlobs = $blobList | Where-Object { $_.LastModified -gt ((Get-Date).Date).AddDays($days) }
    Write-Output "Total blobs matching criteria: " $filteredBlobs.Count

    Write-Output "Downloading...."
    $filteredBlobs | ForEach-Object -Parallel {
        Get-AzStorageBlobContent -Container $using:containerName -Blob $_.Name -Context $using:storageContext -Destination $using:downloadLocation
    } -ThrottleLimit $throttleLimit

    Write-Output "Download Complete"
}


Hope this was helpful. Feel free to provide feedback in the comments. Thank you!

Comments

One response to “How to download azure storage blobs using powershell”

  1. Karan Avatar
    Karan

    Saved my day, thank you for providing the code.

    Like

Leave a reply to Karan Cancel reply