How to authenticate AzCopy on Azure

AzCopy should now be downloaded to your computer (If you don’t know how to do this, go back to the last post here). But before you can perform any tasks, it is necessary to authenticate to your Azure subscription to access Azure Storage first.

There are two ways to authenticate AzCopy to your Azure storage accounts – Azure Active Directory or by a Shared Access Signature (SAS) token. In this article, we’ll focus on using Azure AD.

The most common method to authenticate AzCopy is via Azure AD. When using Azure AD, you have several options. Some of these options are:

  • Interactive Login – User is prompted to log in using the browser.
  • Service Principal + password – For non-interactive login. Recommended for automation and scripting.
  • Service Principal + certificate – For non-interactive login. Recommended for automation and scripting.

In this article, you will learn how to authenticate via interactive login. To do so, first, open a command prompt or PowerShell and run the below command. The –tenant-id parameter is optional but recommended, especially if your login account is associated with more than one Azure tenant.

image

Once executed, you will be asked to open a browser and navigate to https://microsoft.com/devicelogin and enter the displayed code. You can see what that will look like below.

05Enter the code from AzCopy into the browser

Once you’ve entered the code into the browser, click Next and proceed to sign in to your account.

03

When sign-in is done, you should see the status shown in the browser and in the terminal similar to what’s shown in the screenshot below.

04

Now that you have all this knowledge, you should now be ready to put AzCopy in action! See you soon folks!

How to Download and Install the AZCopy Tool

Azure-Command-line-Tool-for-Data-Transfer

This article was motivated by the doubt of one of our readers who asked us to explain more about AzCopy, as he had the need to copy files to the Azure Storage and was having issues (I already helped him to solve the issue, doing this through the AzCopy).

AzCopy is a command-line utility that you can use to copy blobs or files to or from a storage account. It’s a great command-line utility that can automate and streamline the process but requires some setup.

In this article, you’re going to learn how to prepare your system to use AzCopy. This includes downloading and Install the AzCopy, I will divide this post in two, starting explaining just about the download and installation of AzCopy. In the next article, I’ll focus on how to authenticate AzCopy on Azure Storage and how to copy files.

The latest and supported version of AzCopy as of this writing is AzCopy v10. AzCopy is available for Windows, Linux, and macOS. In this article, only the Windows AzCopy utility is covered.

Downloading AzCopy: The Manual Way

There are a couple different to download AzCopy. Let’s first do it the manual way. You might use this method if you don’t intend to install AzCopy on many computers at once.

Navigate to this download link–  and it should initiate a download of the zip file. Once downloaded, extract the zip file to the C:\AzCopy or a folder of your choice.

Lastly, add the installation directory to the system path. Refer to the article here if you need to know how to do that. Adding the folder path to the Windows PATH allows you to call the azcopy executable whenever you are in any working directory at the command line.

Downloading AzCopy via PowerShell Script

If you intend to install AzCopy on many machines or simply need to provide instructions for someone else to install it, you can use PowerShell also. Using a PowerShell script simplifies the process down to a single script.

Create a new PowerShell script and copy/paste the below contents into it. You can get an idea of which each section of the script is doing by inspecting the in-line comments.

By default, the below script will place AzCopy in the C:\AzCopy folder. If you’d like to change that, when running the script, use the InstallPath parameter or simply change the default path in the script itself.

Function Install-AzCopy {
[CmdletBinding()]
param(
[Parameter()]
[string]$InstallPath = ‘C:\AzCopy’
)

    # Cleanup Destination
if (Test-Path $InstallPath) {
Get-ChildItem $InstallPath | Remove-Item -Confirm:$false -Force
}

    # Zip Destination
$zip = “$InstallPath\AzCopy.Zip”

    # Create the installation folder (eg. C:\AzCopy)
$null = New-Item -Type Directory -Path $InstallPath -Force

    # Download AzCopy zip for Windows
Start-BitsTransfer -Source “
https://aka.ms/downloadazcopy-v10-windows” -Destination $zip

    # Expand the Zip file
Expand-Archive $zip $InstallPath -Force

    # Move to $InstallPath
Get-ChildItem “$($InstallPath)\*\*” | Move-Item -Destination “$($InstallPath)\” -Force

    #Cleanup – delete ZIP and old folder
Remove-Item $zip -Force -Confirm:$false
Get-ChildItem “$($InstallPath)\*” -Directory | ForEach-Object { Remove-Item $_.FullName -Recurse -Force -Confirm:$false }

    # Add InstallPath to the System Path if it does not exist
if ($env:PATH -notcontains $InstallPath) {
$path = ($env:PATH -split “;”)
if (!($path -contains $InstallPath)) {
$path += $InstallPath
$env:PATH = ($path -join “;”)
$env:PATH = $env:PATH -replace ‘;;’,’;’
}
[Environment]::SetEnvironmentVariable(“Path”, ($env:path), [System.EnvironmentVariableTarget]::Machine)
}
}

Once the script has run, you can then confirm that AzCopy was downloaded successfully. While still in the PowerShell console, listing the files in the install path by running Get-ChildItem -Path $InstallPath replacing whatever folder you used.

If everything went well, you should see the azcopy.exe utility and a license text file.

You can also confirm that the installation path is added to the system path variable by running $env:Path -split ";" and noticing that the install folder shows up at the bottom of the list.

In the example below, C:\AzCopy is listed which means that the location was added successfully.

image

That and everything for today guys, in the next post I will talk about how to authenticate in Azure Storage and how to effectively copy files using AzCopy.