Azure Blob Storage Quickstart: Using the Azure CLI

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.

Prerequisites

1. Sign in to Azure

Open your terminal or command prompt and sign in to your Azure account:

Command:
az login

This command will open a browser window for you to authenticate. Once authenticated, you can close the browser tab.

2. Create a Resource Group

A resource group is a logical container for your Azure resources. Let's create one:

Command:
az group create --name myResourceGroup --location eastus

Replace myResourceGroup and eastus with your desired name and Azure region.

3. Create a Storage Account

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.

Command:
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.

4. Get Storage Account Credentials

You'll need the storage account name and its access key to interact with the storage. Let's retrieve the key:

Command:
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).

Tip: For easier use in subsequent commands, you can set environment variables for your storage account name and key.
# 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"

5. Create a Container

Containers organize blobs within your storage account. Let's create a container named quickstartblobs:

Command:
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.

6. Upload a Blob

First, create a simple text file named hello.txt with some content.

Then, upload this file to your container:

Command:
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.

7. List Blobs in a Container

See the blobs you've uploaded:

Command:
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.

8. Download a Blob

Download the hello.txt blob back to your local machine:

Command:
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.

9. Clean Up Resources

To avoid ongoing charges, you can delete the resource group and all its contained resources:

Command:
az group delete --name myResourceGroup --yes --no-wait

This action is irreversible. Ensure you have backed up any data you wish to keep.

Next Steps

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.