Post

Azure Blob Storage Tutorial for the Azure CLI

The Azure CLI can be used to perform many different operations for working with Azure Blob Storage.

This includes creating containers, listing containers, uploading blobs, listing blobs in a container, downloading blobs, deleting blobs and deleting containers.

In this tutorial, we will go over the Azure CLI commands for interacting with Azure Blob Storage.

This content is available in video form on the Cloud Engineer Skills YouTube channel.

Azure Blob Storage Azure CLI Commands

Creating Azure Resources for an Azure Blob Storage Account

Login via the Azure CLI using the az login command

1
az login

Create a Resource Group using the az group create command

1
az group create -g cloudengineerskills-sa-rg -l eastus

Creating a Storage Account using the az storage account create command

1
az storage account create -n cloudengineerskillssa -g cloudengineerskills-sa-rg -l eastus --sku Standard_LRS

Granting the Storage Blob Data Contributor Role to the signed in user

Next we will grant the Storage Blob Data Contributor Role to the signed in user enabling us to write blobs to the containers within the Azure Storage Account.

Note you will need to replace the <subscription-id>, <resource-group-name>, and <storage-account-name> with your own values

1
az ad signed-in-user show --query id -o tsv | az role assignment create --role "Storage Blob Data Contributor" --assignee "@-" --scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>"

You can check in the Azure Portal that this has been signed correctly under My Permissions.

Note that role changes can take up to 10 minutes to take effect.

Interacting with a Storage Account via the Azure CLI

Create a container in a storage account

1
az storage container create --account-name cloudengineerskillssa -n mystoragecontainer --auth-mode login

List all containers in a storage account

1
az storage container list --account-name cloudengineerskillssa --auth-mode login

Upload a blob to a container

1
az storage blob upload --account-name cloudengineerskillssa --container-name mystoragecontainer --name abc.txt --file abc.txt --auth-mode login

List all blobs in a container

1
az storage blob list --account-name cloudengineerskillssa --container-name mystoragecontainer --output table --auth-mode login

Download a blob

1
az storage blob download --account-name cloudengineerskillssa --container-name mystoragecontainer --name abc.txt --file def.txt --auth-mode login

Delete a blob

1
az storage blob delete -c mystoragecontainer -n abc.txt --account-name cloudengineerskillssa --auth-mode login

Delete a container

1
az storage container delete --account-name cloudengineerskillssa --name mystoragecontainer --auth-mode login

Cleaning up the resources

1
az group delete --name cloudengineerskills-sa-rg

Feature Reading

This post is licensed under CC BY 4.0 by the author.