Transfer objects to/from Azure Blob storage using the Azure CLI

The Azure CLI is used to create and manage Azure resources from the command line or in scripts. This Quickstart details using the Azure CLI to upload and download data to and from Azure Blob storage.

If you don't have an Azure subscription, create a free account before you begin.

Launch Azure Cloud Shell

The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account. Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. There are a few ways to launch the Cloud Shell:

Click Try It in the upper right corner of a code block. Cloud Shell in this article
Open Cloud Shell in your browser. https://shell.azure.com/bash
Click the Cloud Shell button on the menu in the upper right of the Azure portal. Cloud Shell in the portal

If you choose to install and use the CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.4 or later. Run az --version to find the version. If you need to install or upgrade, see Install Azure CLI 2.0.

Create a resource group

Create an Azure resource group with the az group create command. A resource group is a logical container into which Azure resources are deployed and managed.

az group create \
    --name myResourceGroup \
    --location eastus

Create a storage account

Create a general-purpose storage account with the az storage account create command. The general-purpose storage account can be used for all four services: blobs, files, tables, and queues.

az storage account create \
    --name mystorageaccount \
    --resource-group myResourceGroup \
    --location eastus \
    --sku Standard_LRS \
    --encryption blob

Specify storage account credentials

The Azure CLI needs your storage account credentials for most of the commands in this tutorial. While there are several options for doing so, one of the easiest ways to provide them is to set AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY environment variables.

First, display your storage account keys by using the az storage account keys list command:

az storage account keys list \
    --account-name mystorageaccount \
    --resource-group myResourceGroup \
    --output table

Now, set the AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY environment variables. You can do this in the Bash shell by using the export command:

export AZURE_STORAGE_ACCOUNT="mystorageaccountname"
export AZURE_STORAGE_ACCESS_KEY="myStorageAccountKey"

Create a container

Blobs are always uploaded into a container. You can organize groups of blobs similar to the way you organize your files on your computer in folders.

Create a container for storing blobs with the az storage container create command.

az storage container create --name mystoragecontainer

Upload a blob

Blob storage supports block blobs, append blobs, and page blobs. Most files stored in Blob storage are stored as block blobs. Append blobs are used when data must be added to an existing blob without modifying its existing contents, such as for logging. Page blobs back the VHD files of IaaS virtual machines.

First, create a file to upload to a blob. If you're using the Azure cloud shell, use the following in order to create a file: vi helloworld when the file opens, press insert, type "Hello world" and then press Esc and enter :x and press Enter.

In this example, you upload a blob to the container you created in the last step using the az storage blob upload command.

az storage blob upload \
    --container-name mystoragecontainer \
    --name blobName \
    --file ~/path/to/local/file

If you used the previously described method to create a file in your Azure Cloud Shell, you can use this CLI command instead (note that you didn't need to specify a path since the file was created at the base directory, normally you'd need to specify a path):

az storage blob upload \
    --container-name mystoragecontainer \
    --name helloworld
    --file helloworld

This operation creates the blob if it doesn't already exist, and overwrites it if it does. Upload as many files as you like before continuing.

To upload multiple files at the same time, you can use the az storage blob upload-batch command.

List the blobs in a container

List the blobs in the container with the az storage blob list command.

az storage blob list \
    --container-name mystoragecontainer \
    --output table

Download a blob

Use the az storage blob download command to download the blob you uploaded earlier.

az storage blob download \
    --container-name mystoragecontainer \
    --name blobName \
    --file ~/destination/path/for/file

Data transfer with AzCopy

The AzCopy utility is another option for high-performance scriptable data transfer for Azure Storage. You can use AzCopy to transfer data to and from Blob, File, and Table storage.

As a quick example, here is the AzCopy command for uploading a file called myfile.txt to the mystoragecontainer container.

azcopy \
    --source /mnt/myfiles \
    --destination https://mystorageaccount.blob.core.windows.net/mystoragecontainer \
    --dest-key <storage-account-access-key> \
    --include "myfile.txt"

Clean up resources

If you no longer need any of the resources in your resource group, including the storage account you created in this Quickstart, delete the resource group with the az group delete command.

az group delete --name myResourceGroup

Next steps

In this Quickstart, you learned how to transfer files between local disk and a container in Azure Blob storage. To learn more about working with blobs in Azure Storage, continue to the tutorial for working with Azure Blob storage.