How To Manage Azure Storage Account Using Azure CLI

Introduction 

 
Azure CLI is a cross-platform command-line experience for managing various Azure resources.
 
In this article, we will discuss step by step to create an Azure resource group, create and update storage account, create a container, upload and download blob, and many more operations using Azure CLI.
 
Prerequisites
  1. You should have an Azure account with an active subscription. If you don’t have one, please go ahead and create it. It’s free.
  2. Basic understating of Azure storage account.
  3. Azure CLI installed on your PC. To check that, open Command Prompt and type az. If you see the below screen, it means it’s not installed. Please go ahead and download and install from here.
How To Manage Azure Storage Account Using Azure CLI
 
Let’s begin...
 
Open Command Prompt and check if Azure CLI is properly installed or not by using the az command.
 
How To Manage Azure Storage Account Using Azure CLI
 
Now login to your Azure account using the az login command.
 
How To Manage Azure Storage Account Using Azure CLI
 
Pick your Azure account and login.
 
How To Manage Azure Storage Account Using Azure CLI
 
Upon successful login, you will see similar details on the command prompt.
 
How To Manage Azure Storage Account Using Azure CLI
 
All set!
 
If you have multiple subscriptions, you have to set one of them for the current session.
 
az account set –subscription “NameOfYourSubscription”
 
Now we are going to perform various activities on azure storage account using Azure CLI command like create a storage account and create a container in this storage account to upload a blob, to set the access permissions for the container, to list the blobs in the container and how to download a blob and delete a blob.
 
Before creating a storage account, we need to create a resource group.
 
Step 1 - Create a Resource Group
 
To create resource group, execute az group create --location westus --name demoresgrp1
 
Here:
  • AZ resource group create command: az group create
  • Location for the storage account: westus
  • Resource group name we want to create: demoresgrp1
How To Manage Azure Storage Account Using Azure CLI
 
Once the above command is executed, let’s go to the resource group in Azure Portal and click on refresh. Now you can see the newly created resource group.
 
How To Manage Azure Storage Account Using Azure CLI
 
Step 2 - Create Storage Account
 
To create a storage account in a resource group, execute az storage account create --name mystorage200 --resource-group demoresgrp1 --location westus --sku Standard_LRS
 
Here:
  • AZ storage account create command: az storage account create
  • Storage account we want to create: mystorage200
  • Resource group name: demoresgrp1
  • Location for the storage account: westus
  • Replication: Standard_LRS (that's locally redundant storage)

    How To Manage Azure Storage Account Using Azure CLI
Refresh the Azure portal after execution of the above command, and you'll see a newly created storage account.
 
How To Manage Azure Storage Account Using Azure CLI
 
Now if you want to check all storage account for a resource group in command prompt, execute the below command:
 
az storage account list --resource-group demoresgrp1 --query [*].{Name:name,Location:primaryLocation,Kind:kind} --output table
 
How To Manage Azure Storage Account Using Azure CLI
 
Now we want to update storage account by adding tags on it:
 
az storage account update --name mystorage200 --resource-group demoresgrp1 --tags “env=dev”
 
How To Manage Azure Storage Account Using Azure CLI
 
How To Manage Azure Storage Account Using Azure CLI
 
Step 3 - Create a Container on Storage account
 
Now we are ready to create a container on the storage account using the below command:
 
az storage container create --account-name mystorage200 --name democontainer
  • AZ storage container create command: az storage container create
  • Storage account name: mystorage200
  • Container name we want to create: democontainer
Step 4 - Upload a blob in Container
 
Now go ahead and execute below command to upload this blob onto our container. For that, I have created a sample file in C:\Users\ANUPAM known as demo.txt which contains some simple text.
 
az storage blob upload --account-name mystorage200 --container-name democontainer --name clidemo.txt --file demo.txt
  • AZ blob upload command: az storage blob upload
  • Storage account name: mystorage200
  • Container name: democontainer
  • Name that we want to give to our blob : clidemo.txt
  • Name of the file in our local system that we want to upload as a blob onto the container: demo.txt
How To Manage Azure Storage Account Using Azure CLI
 
How To Manage Azure Storage Account Using Azure CLI
 
Let's try to access the uploaded file by copying blob URL from the container and browse it in a new tab.
 
How To Manage Azure Storage Account Using Azure CLI
 
You will see Resource Not Found. That’s because the container that gets created by default has access permission set to private. 
 
How To Manage Azure Storage Account Using Azure CLI
Now in order to change the public access level, we have one more command and that is to go ahead and set the permissions on the container. Let’s go ahead and execute the below command. This will ensure that we enable public access at the blob level.
 
az storage container set-permission --account-name mystorage200 --name democontainer --public-access blob
 
How To Manage Azure Storage Account Using Azure CLI
 
Now access the same URL from the browser again and you will able to view the contents of the file.
 
How To Manage Azure Storage Account Using Azure CLI
 
Now identify a list of blobs in our container using below command and you can see the name of the file and what the type of blob it is.
 
az storage blob list --account-name mystorage200 --container-name democontainer --output table
 
Here we were given the storage account name, the name of the container and how we want to see the output.
 
How To Manage Azure Storage Account Using Azure CLI
 
Step 5 - Download a blob from Container
 
Next, we will go ahead and download the blob from the container using below command,
 
az storage blob download --account-name mystorage200 --container-name democontainer --name clidemo.txt --file blobdownload.txt
 
Here we give the name of the storage account, the container name, and name of the blob that we want to download and what is the name we want to give to our local file (e.g. blobdownload.txt)
 
How To Manage Azure Storage Account Using Azure CLI
 
Now if we look at the directory we will see a downloaded file in place.
 
Step 6 - Delete a blob from container
 
Let’s delete the blob that we created for demo purpose by using below command,
 
az storage blob delete -c democontainer -n clidemo.txt --account-name mystorage200
 
After a successful deletion, look inside the container in the Azure portal.
 
How To Manage Azure Storage Account Using Azure CLI
 
Done!
 
In this article, we have performed various operations on an Azure storage account using Azure CLI. I hope you gained some insight into this topic.