How to use AzCopy with Azure Cli for authentication

A guide to use AZ CLI for authentication for AzCopy that replaces the soon-to-be deprecated AzCopy login feature.

AzCopy is a command-line tool that allows you to copy files to and from Azure storage accounts. It supports various scenarios such as uploading, downloading, synchronizing, and copying data efficiently. However, after version 10.22, AzCopy will deprecate the “AzCopy login” feature, which allowed you to authenticate with Azure using a device code. The primary replacement for this feature is the Azure CLI option, which uses the Azure CLI credentials to authenticate with Azure. In this blog post, we will show you how to use AzCopy with the Azure CLI option and discuss the benefits and drawbacks of this change.

Prerequisites

To use Azcopy with the Azure CLI option, you need to have the following installed on your machine:

  • The latest version of AzCopy, which you can download from here.
  • The latest version of Azure CLI, which you can download from here.

How to use Azcopy with the Azure CLI option

To use Azcopy with the Azure CLI option, you need to set the environment variable $Env:AZCOPY_AUTO_LOGIN_TYPE="AZCLI". This tells AzCopy to use the Azure CLI credentials for authentication. You can set this variable either permanently or temporarily, depending on your preference.

For example, on Windows, you can set it permanently using the following command in PowerShell:

Set-ItemProperty -Path "HKCU:\Environment" -Name "AZCOPY_AUTO_LOGIN_TYPE" -Value "AZCLI"

Or you can set it temporarily in a script using the following command in PowerShell:

$Env:AZCOPY_AUTO_LOGIN_TYPE="AZCLI"

On Linux, you can set it permanently using the following command in Bash:

echo 'export AZCOPY_AUTO_LOGIN_TYPE="AZCLI"' >> ~/.bashrc

Or you can set it temporarily in a script using the following command in Bash:

export AZCOPY_AUTO_LOGIN_TYPE="AZCLI"

Once you have set the environment variable, you need to log in to Azure using the Azure CLI. You can do this by running the following command in your terminal:

az login

This will open a browser window where you can enter your Azure credentials. Once you are logged in, the Azure CLI will cache your credentials for future requests. You can verify that you are logged in by running the following command in your terminal:

az account show

This will display your current subscription and tenant information. You can also switch to a different subscription or tenant using the Azure CLI commands.

# change the active subscription using the subscription name
az account set --subscription "My Demos"
# change the active subscription using the subscription ID
az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

For more details, see here.

Now you are ready to use AzCopy with the Azure CLI option. You can use any of the AzCopy commands as usual, and AzCopy will use the Azure CLI credentials to authenticate with Azure. For example, you can upload a file to a blob container using the following command:

azcopy copy "C:\local\path\to\file.txt" "https://account.blob.core.windows.net/container/file.txt"

Or you can download a file from a blob container using the following command:

azcopy copy "https://account.blob.core.windows.net/container/file.txt" "C:\local\path\to\file.txt"

Benefits and drawbacks of using AzCopy with the Azure CLI option

Using AzCopy with the Azure CLI option has some benefits and drawbacks compared to the deprecated AzCopy login feature. Here are some of them:

  • Benefit: You can use the same credentials for both AzCopy and Azure CLI, which simplifies the authentication process and reduces the need to manage multiple credentials.
  • Benefit: You can leverage the Azure CLI features such as switching subscriptions and tenants, managing resource groups and policies, and using Azure extensions and commands.
  • Benefit: You can use the Azure CLI option on any platform that supports AzCopy and Azure CLI, such as Windows, Linux, and macOS.
  • Benefit: Users can be added to AAD groups which set the proper RBAC permissions for either Storage Blob Data Reader or Storage Blob Data Contributor
  • Drawback: You need to install and update both AzCopy and Azure CLI on your machine, which may increase the disk space and maintenance requirements.
  • Drawback: You need to log in to Azure using the Azure CLI every time you use a new machine or session at some point when cache isn’t utilized, which may not be practical or efficient for some scenarios.
  • Drawback: You need to set the environment variable $Env:AZCOPY_AUTO_LOGIN_TYPE=”AZCLI” on your machine, which may not be convenient or secure for some scenarios.

Conclusion

In this blog post, we showed you how to use AzCopy with the Azure CLI option and discussed the benefits and drawbacks of this upcoming change of “AzCopy login”. We hope that this guide helps you to use AzCopy more effectively and securely.

About the author

Jeff Pigott is a Senior Cloud Solution Architect at Microsoft, with expertise in Azure technologies such as Arc, DevOps, security, storage, and more. He has over 29 years of experience in IT, development, and DevOps, working with various platforms, tools, and languages. He is passionate about learning the newest Microsoft technologies and services, and holds several Azure certifications.