we will cover the basics of Azure Blob Storage and how to use it with the Azure CLI. We will go through the following topics:
- What is Azure Blob Storage?
- How to create an Azure Blob Storage account?
- How to create a container in Azure Blob Storage?
- How to upload a file to Azure Blob Storage?
- How to download a file from Azure Blob Storage?
- How to list files in a container in Azure Blob Storage?
- How to delete a file from Azure Blob Storage?
- How to delete a container in Azure Blob Storage?
Before we start, make sure you have the Azure CLI installed and configured on your machine.
1. What is Azure Blob Storage?
Azure Blob Storage is a cloud-based object storage service that allows you to store unstructured data such as text or binary data like documents, images, videos, and audio files. It is highly scalable and can be used to store and access data from anywhere in the world.
2. How to create an Azure Blob Storage account?
To create an Azure Blob Storage account using the Azure CLI, follow these steps:
- Open a terminal or command prompt and log in to your Azure account using the
az login
command. - Create a new resource group using the
az group create
command. For example, the following command creates a new resource group namedmyResourceGroup
in theeastus
region:
az group create --name myResourceGroup --location eastus
3. Create a new storage account using the az storage account create
command. For example, the following command creates a new storage account named mystorageaccount
in the myResourceGroup
resource group:
az storage account create --name mystorageaccount --resource-group myResourceGroup --location eastus --sku Standard_LRS
Note: The Standard_LRS
sku is used in this example, which stands for “Standard Locally Redundant Storage”. There are other skus available as well, such as “Premium” and “Standard_ZRS” (Zone-Redundant Storage).
3. How to create a container in Azure Blob Storage?
Once you have created a storage account, you can create a container in Azure Blob Storage using the az storage container create
command. For example, the following command creates a new container named mycontainer
in the mystorageaccount
storage account:
az storage container create --name mycontainer --account-name mystorageaccount --account-key <your-account-key>
Note: Replace <your-account-key>
with the account key for your storage account. You can find your account key in the Azure portal under the “Access keys” section of your storage account.
4. How to upload a file to Azure Blob Storage?
To upload a file to Azure Blob Storage, you can use the az storage blob upload
command. For example, the following command uploads a file named myimage.jpg
to the mycontainer
container in the mystorageaccount
storage account:
az storage blob upload --account-name mystorageaccount --account-key <your-account-key> --container-name mycontainer --type block --name myimage.jpg --source /path/to/local/file/myimage.jpg
Note: The --type block
option is used in this example to upload the file as a block blob. There are other types of blobs available as well, such as “page” and “append” blobs.
5. How to download a file from Azure Blob Storage?
To download a file from Azure Blob Storage, you can use the az storage blob download
command. For example, the following command downloads a file named myimage.jpg
from the mycontainer
container in the mystorageaccount
storage account to the current directory:
az storage blob download --account-name mystorageaccount --account-key <your-account-key> --container-name mycontainer --name myimage.jpg --file myimage.jpg
6. How to list files in a container in Azure Blob Storage?
To list the files in a container in Azure Blob Storage, you can use the az storage blob list
command. For example, the following command lists all the blobs in the mycontainer
container in the mystorageaccount
storage account:
az storage blob list --account-name mystorageaccount --account-key <your-account-key> --container-name mycontainer
7. How to delete a file from Azure Blob Storage?
To delete a file from Azure Blob Storage, you can use the az storage blob delete
command. For example, the following command deletes a file named myimage.jpg
from the mycontainer
container in the mystorageaccount
storage account:
az storage blob delete --account-name mystorageaccount --account-key <your-account-key> --container-name mycontainer --name myimage.jpg
8. How to delete a container in Azure Blob Storage?
To delete a container in Azure Blob Storage, you can use the az storage container delete
command. For example, the following command deletes the mycontainer
container from the mystorageaccount
storage account:
az storage container delete --account-name mystorageaccount --account-key <your-account-key> --name mycontainer
Note: Deleting a container will also delete all the files in the container.
That’s it! You now know how to create an Azure Blob Storage account, create a container, upload and download files, list files in a container, and delete files and containers using the Azure CLI. Azure Blob Storage is a powerful and flexible service that can be used to store and access unstructured data in the cloud.