This guide will walk you through the fundamental steps of interacting with Azure Blob Storage using the Azure Command-Line Interface (CLI). You'll learn how to create a storage account, upload a file, download a file, and list blobs.
Open your terminal or command prompt and sign in to your Azure account:
az login
This command will open a browser window for you to authenticate. Once authenticated, you can close the browser tab.
A resource group is a logical container for your Azure resources. Let's create one:
az group create --name myResourceGroup --location eastus
Replace myResourceGroup
and eastus
with your desired name and Azure region.
Now, create a storage account. The storage account name must be globally unique and between 3 and 24 characters in length, using numbers and lowercase letters only.
az storage account create --name mystorageaccountname --resource-group myResourceGroup --location eastus --sku Standard_LRS
Remember to replace mystorageaccountname
with a unique name. Standard_LRS
specifies Locally-redundant storage, which is a cost-effective option.
You'll need the storage account name and its access key to interact with the storage. Let's retrieve the key:
az storage account keys list --account-name mystorageaccountname --resource-group myResourceGroup --query "[0].value" --output tsv
Copy the output of this command; this is your primary access key. You'll also need your storage account name (e.g., mystorageaccountname
).
# On Linux/macOS
export AZURE_STORAGE_ACCOUNT="mystorageaccountname"
export AZURE_STORAGE_KEY="your_access_key_here"
# On Windows Command Prompt
set AZURE_STORAGE_ACCOUNT=mystorageaccountname
set AZURE_STORAGE_KEY=your_access_key_here
# On Windows PowerShell
$env:AZURE_STORAGE_ACCOUNT="mystorageaccountname"
$env:AZURE_STORAGE_KEY="your_access_key_here"
Containers organize blobs within your storage account. Let's create a container named quickstartblobs
:
az storage container create --name quickstartblobs --account-name mystorageaccountname --auth-mode login
Using --auth-mode login
leverages your Azure CLI login credentials for authentication, which is generally more secure than using the access key directly in commands when possible.
First, create a simple text file named hello.txt
with some content.
Then, upload this file to your container:
az storage blob upload --file hello.txt --container-name quickstartblobs --name hello.txt --account-name mystorageaccountname --auth-mode login
This uploads the local file hello.txt
to a blob with the same name in the quickstartblobs
container.
See the blobs you've uploaded:
az storage blob list --container-name quickstartblobs --account-name mystorageaccountname --output table --auth-mode login
This will display a table listing your blobs, including hello.txt
.
Download the hello.txt
blob back to your local machine:
az storage blob download --file hello_downloaded.txt --container-name quickstartblobs --name hello.txt --account-name mystorageaccountname --auth-mode login
This downloads the blob hello.txt
and saves it as hello_downloaded.txt
locally.
To avoid ongoing charges, you can delete the resource group and all its contained resources:
az group delete --name myResourceGroup --yes --no-wait
This action is irreversible. Ensure you have backed up any data you wish to keep.
You've successfully completed a basic quickstart using Azure CLI for Blob Storage. Explore these resources to learn more:
Remember to secure your storage account access keys. For production scenarios, consider using Managed Identities or Azure Role-Based Access Control (RBAC) for more granular and secure access.