Upload and Download Azure Blob Storage using AZCopy

Upload and Download Azure Blob Storage using AZCopy

AZCopy is a command-line utility for uploading and downloading Azure Blob Storage files.

At the time of writing, the most recent version of AZCopy is V10. So, you can get the AZCopy executable file from the link below.

https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10

If the link does not work, look for the most recent version of AZCopy and download the executable file. Download the file and save it in a location that is convenient for you on your computer.

In this blog, I'll show you how to use an SAS token to upload and download files from Azure Blob Storage.

Get SAS token

First, let's look at how to obtain an SAS token.

  1. Go to the Azure Storage Account to which you want to upload or download files.
  2. On the right side, under Security + networking, choose Shared access signature.
  3. Locate the Allowed resource types and choose the preferred resource type. I'm going with all three options (Service, Container, Object)
  4. Enter the start and expiry date and times. That is when the authorization will begin to work and when it will expire. You can only access the resource for two days if it is two days.
  5. Select the Generate SAS and connection string option.
  6. Make a copy of the SAS token
Get SAS token

Download a Specific File

To obtain a file for download. Launch a command prompt and then relocate the AZCopy folder (which you downloaded). The syntax for downloading a file is as follows.

azcopy copy "https://[account].blob.core.windows.net/[container]/[path-to-blob]?[SAS token]" "[path-to-file]"
  • [account] - Name of the storage account
  • [container]/[path-to-blob] - path and container name
  • [SAS token] - Which you generated and copied from the Azure site.
  • [path-to-file] - Path to download a file in local machine

A sample command prompt script is provided below.

azcopy copy  "https://customerstorage.blob.core.windows.net/sample/SamplePDF 1.pdf?sv=2021-06-08&ss=bt&srt=so&sp=ratfx&se=2023-01-06T22:10:33Z&st=2023-01-06T14:10:33Z&spr=https&sig=KZmEkzE" "C:\Sample\SamplePDF 1.pdf"

The output of the above command prompt script is shown below.

AZCopy

Upload a Specific File

The upload procedure is similar to the download procedure. Only the order will be altered. Always put the source file name first.

azcopy copy "[path-to-file]" "https://[account].blob.core.windows.net/[container]/[path-to-blob]?[SAS token]"

A command prompt sample script is provided below.

azcopy copy "C:\Sample\SamplePDF 1.pdf"  "https://customerstorage.blob.core.windows.net/sample/SamplePDF 1.pdf?sv=2021-06-08&ss=bt&srt=so&sp=ratfx&se=2023-01-06T22:10:33Z&st=2023-01-06T14:10:33Z&spr=https&sig=KZmEkzE"

Download all of the files in the specified container

Let's look at how to download the specific container files to your local machine next. The syntax for downloading all containers from a specific file is as follows.

azcopy copy "https://[account].blob.core.windows.net/[container]/*?[SAS token]" "[local machine path]" --recursive

A command prompt script for downloading entire container files is provided below. To copy the folder structure, the –recursive attribute was added.

azcopy copy "https://customerstorage.blob.core.windows.net/sample/*?sv=2021-06-08&ss=bqt&srt=so&sp=rwdlacupiytfx&se=2023-01-09T19:28:35Z&st=2023-01-09T11:28:35Z&spr=https&sig=%2FZY9FlKkJcwZqU%2FojimSaVAg%3D" "C:\Sample" --recursive
AZCopy

Upload all of the files in the specified folder to Azure blob storage container

The syntax below is for uploading all folder files to an Azure blob storage container.

azcopy copy "[local machine path]\*" "https://[account].blob.core.windows.net/[container]?[SAS token]" --recursive

The following is an example command prompt script that will upload all of the files in a specific folder to an Azure blob storage container.

azcopy copy "C:\Sample\*" "https://customerstorage.blob.core.windows.net/sample?sv=2021-06-08&ss=bqt&srt=so&sp=rwdlacupiytfx&se=2023-01-09T19:28:35Z&st=2023-01-09T11:28:35Z&spr=https&sig=%2FZY9FlKkJcwZqU%2FojimSaVAg%3D" --recursive

Copy from one Azure Blob Storage to another Azure Blob Storage

You can directly copy one container file to another container. The syntax for that is as follows:

azcopy copy "https://<source-account>.blob.core.windows.net/<source-container>/*?<source-SAS token>" "https://<destination-account>.blob.core.windows.net/<<destination-container>/*?<<destination-SAS token>"  --recursive

I hope this helps you. Keep coding.

Comments

Popular posts from this blog

Entity Framework Core (EF) with SQL Server LocalDB

SignalR with JavaScript in ASP.NET Core MVC

Localization in ASP.NET Core MVC with Example