azure,

How to manage blobs using Azure CLI

Sep 30, 2019 · 4 mins read · Post a comment
How to manage blobs using Azure CLI

In the last Azure post we have discussed the different types of Azure storage accounts and created one using the Azure CLI. Next, we are going to upload, download and list blob files named Blobs.
Blobs is a REST-based object storage service that is used for storing large ammount of unstructured data, such as text, or binaries. The most common scenario for Blob Storage is serving static website using CDN (Content Delivery Network). Other examples may include:

  • Storing media files
  • Streaming
  • Storing data for analysis
  • Storing files for backups, disaster recovery, archiving or other purposes.

Blobs are comprised of many blocks, each identified by a block ID. There are 3 types of blobs:

  • block blobs: Optimized for uploading larger blobs.
  • page blobs: Optimized for random read and write operations. Maximum size for page blobs is 8 TB.
  • append blobs: Optimized for append operations. Append blob can have arount 195 GB with up to 50000 blocks with up to 4 MB in size.

More about Azure Blob types and Azure Blob Storage.

Prerequisites

  • Azure account
  • Azure CLI
  • Azure storage account

Configure storage account credentials

Step 1. Open Terminal and login to the Azure Portal:

az login

It will open a new window using the default browser where you will be prompted for email and password.

Step 2. Before doing any operation on the storage account, we need the storage account credentials. There are several ways to provide these credentials, but the easiest and not so secure one is using AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_KEY environment variables. List the storage account keys:

az storage account keys list --account-name devcoops1 --resource-group storage-rg --output table

The output should look like this:
Azure CLI storage account keys list

Step 3. Export the environment variables including a storage key listed in Step 2, or create a new key:

export AZURE_STORAGE_ACCOUNT="devcoops1"
export AZURE_STORAGE_KEY="<insert_a_storage_key_here>"

Note: Make sure the environment variables exports are correct by running echo $AZURE_STORAGE_ACCOUNT and echo $AZURE_STORAGE_KEY.

Create a blob container

Step 4. Azure Blobs are always stored and grouped in containers. Think of them as folders or directories where files are stored. Create a blob container using the following command:

az storage container create --name myfirstblobcontainer

JSON response message:

{ "created": true }

Note: If an error message pop-up: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')), there are a few workarounds:

  1. Add 0.0.0.0 localhost line to the hosts file. Don’t forget to remove the line once you are done with the example.
  2. Connect the Azure Cloud Shell to the Visual Studio Code using the instructions on this post.
  3. Open the Azure Cloud Shell from the Azure Portal.

Upload a blob

Step 5. First, we are going to create a file before the upload to a blob. Create a sample “Hello World” file index.php:

echo "<?php echo "Hello World\!";?>" > index.php

Step 6. Next, upload the file:

az storage blob upload --container-name myfirstblobcontainer --name helloworld --file index.php

List the blob file

Step 7. Now, we are going to confirm the file upload by listing the container:

az storage blob list --container-name myfirstblobcontainer --output table

Output: Azure CLI list blob container

Download the blob file

Step 8. You could also download the blob file by using the command:

az storage blob download --container-name myfirstblobcontainer --name helloworld --file index.php

Cleanup

If you aren’t using the storage account anymore, you could delete the resource group anytime using the following command:

az group delete --name storage-rg

Next steps

Instead of using storage access key, we’ll configure a shared access signature known as SAS token.

Official documentation: Upload, download, and list blobs using the Azure CLI.

Join Newsletter
Get the latest news right in your inbox. We never spam!