Script to download files from Azure Storage account which specified in csv file

Abduazim Sobitov 81 Reputation points
2022-08-04T02:46:54.973+00:00

Hi team,

Newbie in scripting here. We have a storage account where we store nearly thousand files. Each week I have a list of files which I need to download in order to complete some manual workaround from this storage account.

The list of files is getting bigger, nearly 300 files now. Is there a way to write a script using Azure CLI to download specific list of files.

Thank for help

Azure Storage
Azure Storage

Globally unique resources that provide access to data management services and serve as the parent namespace for the services.

0 comments No comments

2 answers

Sort by: Oldest
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. ManojDumpa-0731 0 Reputation points
    2026-11-24T18:21:20.5466667+00:00

    Hello, you can try the following process. This assumes you're using Azure Blob Storage, not Azure File Shares.

    1. **Create **files.txt

    Create a text file named files.txt and add one blob name/path per line:

    file001.csv

    file002.csv

    file003.xlsx

    folder1/file004.csv

    folder2/file005.txt

    For the weekly process, simply add all ~300 required blob names to this file. The script will process only the blobs listed in files.txt.

    **Create **Download-Files.ps1

    Save the following as Download-Files.ps1:

    ==========================================

    Azure Blob Weekly Download Script

    ==========================================

    #Storage details

    $account   = "YOUR_STORAGE_ACCOUNT"

    $container = "YOUR_CONTAINER_NAME"

    #Input and output locations

    $fileList       = ".\files.txt"

    $downloadFolder = ".\Downloads"

    #Counters

    $downloaded = 0

    $skipped    = 0

    $failed     = 0

    #Validate input file

    if (-not (Test-Path -LiteralPath $fileList)) {

        Write-Host "ERROR: files.txt not found." -ForegroundColor Red

        exit 1

    }

    #Create main download folder

    New-Item -ItemType Directory -Force -Path $downloadFolder | Out-Null

    #Process filenames

    foreach ($line in Get-Content -LiteralPath $fileList) {

        $blob = $line.Trim()

        # Ignore blank lines

        if ([string]::IsNullOrWhiteSpace($blob)) {

            continue

        }

        Write-Host ""

        Write-Host "Processing: $blob"

        # Reject potentially unsafe local paths

        if ($blob -match '(^|/|\)..(/|\|$)') {

            Write-Host "SKIPPED - unsafe path: $blob" -ForegroundColor Yellow

            $skipped++

            continue

        }

        # Preserve the blob folder structure locally

        $relativePath = $blob.Replace('/', [IO.Path]::DirectorySeparatorChar)

        # Build local destination

        $localFile = Join-Path $downloadFolder $relativePath

        # Create required local subfolders

        $localDirectory = Split-Path $localFile -Parent

        if (-not (Test-Path -LiteralPath $localDirectory)) {

            New-Item -ItemType Directory -Force -Path $localDirectory | Out-Null

        }

        # Do not overwrite an existing local file

        if (Test-Path -LiteralPath $localFile) {

            Write-Host "SKIPPED - already exists: $localFile" -ForegroundColor Yellow

            $skipped++

            continue

        }

        Write-Host "Downloading..."

        az storage blob download `

            --account-name $account `

            --container-name $container `

            --name $blob `

            --file $localFile `

            --auth-mode login `

            --overwrite false `

            --only-show-errors

        if ($LASTEXITCODE -eq 0) {

            Write-Host "SUCCESS: $blob" -ForegroundColor Green

            $downloaded++

        }

        else {

            Write-Host "FAILED: $blob" -ForegroundColor Red

            $failed++

        }

    }

    #Summary

    Write-Host ""

    Write-Host "===================================="

    Write-Host "Download process completed"

    Write-Host "===================================="

    Write-Host "Downloaded : $downloaded" -ForegroundColor Green

    Write-Host "Skipped    : $skipped" -ForegroundColor Yellow

    Write-Host "Failed     : $failed" -ForegroundColor Red

    Write-Host "===================================="

    if ($failed -gt 0) {

        exit 1

    }

    else {

        exit 0

    }

    ###########################################################################

    The important point is that az storage blob download is being used to download each listed blob. This script does not contain commands to delete, upload, rename, move, or modify blobs in the storage account.

    --auth-mode login uses the identity signed into Azure CLI.

    3. Update these two values

    Change:

    $account   = "YOUR_STORAGE_ACCOUNT"

    $container = "YOUR_CONTAINER_NAME"

    For example:

    $account   = "mystorageaccount"

    $container = "inputfiles"

    4. Folder structure

    Before running:

    WeeklyDownload

    │

    ├── Download-Files.ps1

    └── files.txt

    5. Sign in and run

    Open PowerShell in the WeeklyDownload directory and sign in:

    az login

    Then run:

    .\Download-Files.ps1

    The signed-in account needs permission to read the required blobs. For a download-only process, use the least privilege available, such as Storage Blob Data Reader, rather than running with broad Owner/Contributor privileges unnecessarily.

    You also should not normally need to run PowerShell as Windows Administrator if you have normal write access to the download directory.

    6. Result

    If files.txt contains:

    file001.csv

    folder1/file004.csv

    folder2/file005.txt

    the result will be:

    WeeklyDownload

    │

    ├── Download-Files.ps1

    ├── files.txt

    └── Downloads

        ├── file001.csv

        ├── folder1

        │   └── file004.csv

        └── folder2

            └── file005.txt

    The folder structure is intentionally preserved. Therefore, if Blob Storage contains:

    folder1/report.csv

    folder2/report.csv

    both can be downloaded safely as:

    Downloads\folder1\report.csv

    Downloads\folder2\report.csv

    et me know how it works. I recommend testing with 2–3 files initially, and once everything is working as expected, gradually scale up to the full set.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.