Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to List Files in Azure Blob with az CLI?

Learn how to get a list of files in Azure Blob Storage using the az CLI tool. Discover the latest working commands and avoid deprecated ones.
Azure CLI terminal displaying blob storage file listing with a futuristic cloud storage background. Azure CLI terminal displaying blob storage file listing with a futuristic cloud storage background.
  • 🚀 The az CLI allows efficient and automated management of Azure Blob Storage directly from the command line.
  • 🔍 Azure Blob Storage supports block, append, and page blobs, with block blobs being the most common for file storage.
  • ⚡ Running az storage blob list simplifies retrieving file lists from storage containers with filtering and formatting options.
  • 🔐 Secure access with SAS tokens ensures safe authentication without exposing credentials.
  • 🛠️ For large datasets, pagination, filtering, and query parameters improve efficiency in listing files.

How to List Files in Azure Blob Storage Using az CLI (Step-by-Step Guide)

Azure Blob Storage is a cloud-based object storage solution that allows developers to store and manage unstructured data such as images, videos, and backups. The Azure Command-Line Interface (CLI) provides a powerful way to interact with Blob Storage efficiently. Using the az CLI, developers can list files in Azure Blob Storage with speed and precision, eliminating the need to navigate the Azure portal. In this guide, we’ll cover all the necessary steps to use the az CLI to retrieve file lists, explore advanced commands, and ensure security best practices.

Understanding Azure Blob Storage

Azure Blob Storage is an essential solution for managing vast amounts of unstructured data on the cloud. It provides high availability, scalability, and integration with various services in the Azure ecosystem.

Types of Blobs in Azure Storage

  1. Block Blobs – These are the most commonly used for storing text or binary files such as images, documents, and log files.
  2. Append Blobs – Optimized for sequential write operations, typically used for logging applications or audit trails.
  3. Page Blobs – Designed for random read/write operations, primarily used for virtual hard disks in Azure Virtual Machines.

When listing files in Azure Blob Storage, most use cases revolve around Block Blobs, as they store typical file-based data.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Why Use the az CLI for Blob Storage Management?

Managing Azure Blob Storage through the CLI can significantly improve efficiency for cloud administrators and developers.

  • Speed & Efficiency – Executing a simple command is often faster than navigating the Azure portal.
  • Automation & Scripting – Enables repeatable scripts, making it easy to integrate with CI/CD pipelines.
  • Remote Accessibility – Access from any system with the CLI installed, making it ideal for cloud-native applications.
  • Control & Customization – Filter results, format outputs, and automate workflows using CLI queries.

Prerequisites for Using az CLI with Azure Blob Storage

Before you begin, ensure you meet these requirements:

  1. Have an Azure Account – You need an active Azure subscription. You can sign up here.
  2. Install the Azure CLI – Download and install from the official Microsoft page.
  3. Authenticate with Azure – To sign in, use:
    az login
    

    Then, set your preferred subscription:

    az account set --subscription <subscription_id>
    

Steps to List Files in Azure Blob Storage Using az CLI

Step 1: Identify Your Storage Account and Container

Every file in Azure Blob Storage resides within a container inside a storage account. To find your storage account:

az storage account list --output table

Then, to list available containers within a particular storage account:

az storage container list --account-name <storage_account> --output table

Step 2: Retrieve a List of Files (Blobs) in a Container

To get a list of all stored files (blobs) inside a container:

az storage blob list --account-name <storage_account> --container-name <container_name> --output table

For structured data in JSON format:

az storage blob list --account-name <storage_account> --container-name <container_name> --output json

Advanced Listing Techniques

Filtering Results with Queries

The Azure CLI allows filtering data using the --query parameter for refined results. Example:

az storage blob list --account-name <storage_account> --container-name <container_name> --query "[].{Name:name, Size:properties.contentLength}" --output table

Listing Files in Subfolders (Recursive Listing)

For cases where blob names include virtual folder paths, use:

az storage blob list --account-name <storage_account> --container-name <container_name> --delimiter "/" --output table

Handling Authentication & Secure Access with SAS Tokens

Instead of using storage account keys, generate a Shared Access Signature (SAS) token:

az storage blob generate-sas --account-name <storage_account> --container-name <container_name> --permissions rl --expiry 2024-12-31 --output table

The SAS token ensures secure, temporary access without exposing sensitive credentials in scripts.

Integrating az storage blob list into Development & DevOps Workflows

Many organizations incorporate Azure CLI commands into CI/CD and automation pipelines:

  • Verifying file existence before deployment
  • Automating backup reports
  • Triggering Azure Functions upon file changes

Example script checking if a specific file exists:

blobs=$(az storage blob list --account-name myStorage --container-name myContainer --query "[].name" --output tsv)
if [[ $blobs == *"config.json"* ]]; then
    echo "Config file found!"
else
    echo "Missing config file!"
fi

Troubleshooting Common Issues

1. Authentication Errors

If az login fails, try using:

az login --use-device-code

Validate current authentication:

az account show

2. Permission Issues

Confirm access rights with:

az role assignment list --assignee <your-email-or-service-principal>

To grant Storage Blob Data Reader role:

az role assignment create --assignee <your-email-or-service-principal> --role "Storage Blob Data Reader" --scope /subscriptions/<subscription_id>/resourceGroups/<resource_group>/providers/Microsoft.Storage/storageAccounts/<storage_account>

3. Network & Firewall Issues

Check access restrictions:

az storage account show --name <storage_account> --query "networkRuleSet"

4. Handling Large File Listings Efficiently

For large storage accounts, limit results using pagination:

az storage blob list --account-name <storage_account> --container-name <container_name> --num-results 100

To retrieve results incrementally, the --marker flag allows continuation from a previous position.

Conclusion

Azure CLI (az CLI) is a robust and flexible tool for managing Azure Blob Storage. By following this guide, developers can efficiently list files, work with filtered results, and securely access storage without exposing sensitive credentials. Ensure you're using the latest CLI version, apply best practices for access management, and integrate command-line automation for maximum efficiency in cloud storage management.


Citations

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading