Categories

Sunday, December 3, 2017

Azure CLI - Azure Blob Storage Other Useful Commands

In this tutorial I have executed all the below mentioned commands in Azure CLI. You can comment or uncomment any line as per the requirement.

In this tutorial we will cover below commands:

This must be the very first line in your bash script.

#!/bin/bash

Login to Azure subscription account

In the first step you have to login to your Azure subscription account using below mentioned command. You can skip this command if already logged-in.

az login

After executing the command  you will see similar message on console.

"to sign in, use a web browser to open the page https://aka.ms/devicelogin and enter the code #h98mdtxxk to authenticate."

Declare all variables at one place

In the second step declare all the required variables at one place. So you can reuse them anywhere in the program and it is easier to find declared variables value.

resourceGroup="azurecertifications-resource-group"
location="southeastasia"
blobStorageAccount="azstorageaccount02"
containerNameImage="images"
containerNameThumbnail="thumbs"
containerImagePublicAccessLevel="container"


Retrieve Blob Storage Account Access key

Below command will return Storage Account 'key1 in key=value name-pair format and then we will parse it by '=' delimiter. Storage account key will be needed to execute all other commands which we will discuss here in this tutorial.

output=$(az storage account keys list --resource-group $resourceGroup --account-name $blobStorageAccount --query [0].value --output tsv)

blobStorageAccountKey=$( cut -d '=' -f 1- <<< "$output" )


Enable public read access for your container

A newly created container is private by default. That is, nobody can access the container without a shared access signature or the access keys for the storage account. Using Azure CLI, you can modify this behavior by setting container permissions to one of three levels:

--public-access off      (Default) No public read access

--public-access blob      Public read access to blobs only

--public-access container      Public read access to blobs, can list blobs in container


az storage container set-permission --account-name $blobStorageAccount --name $containerNameImage --account-key $blobStorageAccountKey --public-access $containerImagePublicAccessLevel

Upload a blob to a container

This operation creates the blob if it doesn't already exist, and overwrites it if it does.

az storage blob upload --account-name $blobStorageAccount --container-name $containerNameImage --account-key $blobStorageAccountKey --name $containerNameImage --file "C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg"

Lists the blob in a container

This command will display all the blob names exists in a container.

az storage blob list --account-name $blobStorageAccount --container-name $containerNameImage --account-key $blobStorageAccountKey --output table



Download the blob

az storage blob download --account-name $blobStorageAccount --container-name $containerNameImage --account-key $blobStorageAccountKey --name "Chrysanthemum.jpg" --file ~/destination/path/for/file

Copy blob within storage account

We can copy blobs within or across storage accounts and regions asynchronously. Make sure destination container must exist before copying.

az storage blob copy start --account-name $blobStorageAccount --account-key $blobStorageAccountKey --destination-blob "Desert.jpeg" --destination-container $containerNameThumbnail --source-uri "https://azstorageaccount02.blob.core.windows.net/images/Desert.jpg"

After copy you will find copied image in destination container. For this tutorial I have verified this using Azure Portal.



Delete a blob

az storage blob delete --account-name $blobStorageAccount --container-name $containerNameImage --account-key $blobStorageAccountKey --name "Chrysanthemum.jpg"

Set the content type to blob file

The content type, also known as the MIME type, identitifies the format of the data in the blob. Browsers and other software use the content type to determine how to process the data. The following example sets the content type from image/jpeg to image/png.

az storage blob update --account-name $blobStorageAccount --container-name $containerNameImage --account-key $blobStorageAccountKey --name "Desert.jpg" --content-type image/png







Get the URL for the blob

az storage blob url --account-name $blobStorageAccount --container-name $containerNameImage --account-key $blobStorageAccountKey --name "Desert.jpg" --output tsv

Command output:   https://azstorageaccount02.blob.core.windows.net/images/Desert.jpg

System Properties and User-defined metadata

Objects in Azure Storage support system properties and user-defined metadata, in addition to the data they contain.

  • System properties: System properties exist on each storage resource. Some of them can be read or set, while others are read-only. Some system properties correspond to certain standard HTTP headers. The Azure storage client library maintains these for us.

  • User-defined metadata: User-defined metadata is metadata that we specify on a given resource in the form of a name-value pair. We can use metadata to store additional values with a storage resource. These additional metadata values are for our own purposes only, and do not affect how the resource behaves.
Show System properties of a blob

Each blob has several service-defined properties including its name, type, length, and others. We can also configure a blob with our own properties and their values.

az storage blob show --account-name $blobStorageAccount --container-name $containerNameImage --account-key $blobStorageAccountKey --name "Desert.jpg" --output table

Set User-defined metadata of a blob

az storage blob metadata update --account-name $blobStorageAccount --container-name $containerNameImage --account-key $blobStorageAccountKey --name "Desert.jpg" --metadata "author=azurecertifications@gmail.com" "type=blobfile"

Display blob metadata

az storage blob metadata show --account-name $blobStorageAccount --container-name $containerNameImage --account-key $blobStorageAccountKey --name "Desert.jpg" --output json

Command output:   "author=azurecertifications@gmail.com" "type=blobfile"

You can verify the same using Azure Portal. See below screenshot.





All the commands we have executed in Azure CLI here and you can do the same and manage Azure blob storage resources using Azure Storage Explorer. Click on this link for more details about Azure Storage Explorer.



Related Articles:



No comments:

Post a Comment