Quickstart: Upload, download, and list blobs using the Azure CLI
The Azure CLI is Azure's command-line experience for managing Azure resources. You can use it in your browser with Azure Cloud Shell. You can also install it on macOS, Linux, or Windows and run it from the command line. In this quickstart, you learn to use the Azure CLI to upload and download data to and from Azure Blob storage.
To access Azure Storage, you'll need an Azure subscription. If you don't already have a subscription, then create a free account before you begin.
All access to Azure Storage takes place through a storage account. If you don't already have a storage account, you can easily create one using the Azure portal. You can also create a storage account using Azure PowerShell or Azure CLI. For help creating the account, see Create a storage account.
Open Azure Cloud Shell
Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. Just select the Copy button to copy the code, paste it in Cloud Shell, and then press Enter to run it. There are a few ways to open Cloud Shell:
|Select Try It in the upper-right corner of a code block.|
|Open Cloud Shell in your browser.|
|Select the Cloud Shell button on the menu in the upper-right corner of the Azure 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 determine your version. If you need to install or upgrade, see Install the Azure CLI.
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_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_ACCESS_KEY environment variables. You can do this in the Bash shell by using the
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
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.