Azure Storage CLI Documentation
Introduction
The Azure CLI (Command-Line Interface) provides a powerful and flexible way to manage your Azure Storage resources directly from your terminal. This documentation guides you through the common commands for interacting with Blob, File, Table, and Queue storage services.
Whether you're automating deployments, performing routine maintenance, or analyzing data, the Azure CLI is an indispensable tool for any Azure developer or administrator.
Installation
Before you begin, ensure you have the Azure CLI installed. You can find detailed installation instructions for your operating system on the official Azure CLI documentation.
After installation, log in to your Azure account:
az login
Managing Blob Storage
Azure Blob Storage is designed for storing massive amounts of unstructured data, such as text or binary data. The Azure CLI offers comprehensive commands for managing blobs and containers.
Create a Blob Container
Containers are logical groupings of blobs. To create a new container, use the az storage container create
command:
az storage container create --name mycontainer --account-name mystorageaccount --auth-mode login
Replace mycontainer
with your desired container name and mystorageaccount
with your storage account name. Using --auth-mode login
leverages your Azure CLI login credentials.
Upload a Blob
Upload a file as a blob using az storage blob upload
:
az storage blob upload --container-name mycontainer --file ./local/path/to/myfile.txt --name remote/blob/name.txt --account-name mystorageaccount --auth-mode login
--file
specifies the local path, and --name
specifies the desired name for the blob in the container.
Download a Blob
Download a blob to your local machine with az storage blob download
:
az storage blob download --container-name mycontainer --name remote/blob/name.txt --file ./downloaded/myfile.txt --account-name mystorageaccount --auth-mode login
List Blobs
List all blobs within a container:
az storage blob list --container-name mycontainer --account-name mystorageaccount --auth-mode login
Delete a Blob
Delete a specific blob:
az storage blob delete --container-name mycontainer --name remote/blob/name.txt --account-name mystorageaccount --auth-mode login
To delete an entire container, use az storage container delete
.
Managing File Storage
Azure Files offers fully managed cloud file shares that are accessible via the industry-standard Server Message Block (SMB) protocol. You can mount these shares on cloud or on-premises Windows, macOS, and Linux systems.
Create a File Share
Create a new file share using az storage share create
:
az storage share create --name myshare --account-name mystorageaccount --auth-mode login
Upload a File
Upload a file to a share directory using az storage file upload
:
az storage file upload --share-name myshare --source ./local/path/to/document.pdf --path documents/ --account-name mystorageaccount --auth-mode login
The --path
argument specifies the directory within the share.
Download a File
Download a file from a share:
az storage file download --share-name myshare --path documents/document.pdf --dest ./downloaded/document.pdf --account-name mystorageaccount --auth-mode login
List Files
List files and directories within a share:
az storage file list --share-name myshare --path documents/ --account-name mystorageaccount --auth-mode login
Delete a File
Delete a file from a share:
az storage file delete --share-name myshare --path documents/document.pdf --account-name mystorageaccount --auth-mode login
To delete an entire file share, use az storage share delete
.
Managing Table Storage
Azure Table Storage is a NoSQL key-attribute store that lets you store large amounts of unstructured data. The Azure CLI can interact with tables and entities.
Create a Table
Create a new table using az storage table create
:
az storage table create --name mytable --account-name mystorageaccount --auth-mode login
Insert an Entity
Insert a new entity into a table. Note that table storage doesn't have fixed schemas. You define entities as sets of key-value properties.
Example: Insert a user entity
az storage entity insert --table mytable --account-name mystorageaccount --partition-key user --row-key 123 --columns Name=Alice Age=30 Email=alice@example.com --auth-mode login
--partition-key
and --row-key
form the unique identifier for an entity.
Query Entities
Query entities from a table using az storage entity query
. You can specify filter expressions.
Example: Query entities with Age greater than 25
az storage entity query --table mytable --account-name mystorageaccount --filter Age gt 25 --auth-mode login
Delete a Table
Delete an entire table and all its entities:
az storage table delete --name mytable --account-name mystorageaccount --auth-mode login
Managing Queue Storage
Azure Queue Storage is a service that stores large numbers of messages. Queue messages can be accessed from anywhere in the world via HTTP or HTTPS.
To interact with queues, you typically use commands like az storage queue create
, az storage message put
, az storage message get
, and az storage queue delete
.
az storage queue
and az storage message
.
Authentication
The Azure CLI supports several authentication methods:
az login
: Authenticates using your Azure Active Directory credentials. This is the recommended method for interactive use.- Shared Access Signatures (SAS): Generate temporary access credentials for specific permissions and expiry times. Use these with the appropriate connection string or account key parameters.
- Account Keys: Direct access using storage account keys. While convenient, this method grants full access and should be used cautiously, especially in scripts.
In the examples above, --auth-mode login
leverages your AAD credentials. For other methods, you might use --connection-string
or --account-key
.
Best Practices
- Use
--auth-mode login
: Whenever possible, use interactive login for enhanced security. - Permissions: When using SAS tokens or managed identities, grant the minimum necessary permissions.
- Error Handling: Implement robust error handling in your scripts to manage command failures gracefully.
- Resource Naming: Follow Azure naming conventions for storage accounts, containers, and queues for consistency.
- Idempotency: Design your scripts to be idempotent, meaning running them multiple times has the same effect as running them once.
--query
parameter with az storage blob list
or az storage file list
to filter and shape the output of your commands, making it easier to parse in scripts.