Published on

February 11, 2020

How to Create Containers and Upload Files in Azure Blob Storage using AzCopy

Azure Blob Storage is a popular cloud storage solution provided by Microsoft Azure. It allows you to store and retrieve large amounts of unstructured data, such as images, videos, and documents. AzCopy is a command-line tool that simplifies the process of uploading and downloading files to and from Azure Blob Storage.

Generating a Shared Access Signature (SAS)

Before we can start using AzCopy to interact with Azure Blob Storage, we need to generate a Shared Access Signature (SAS). A SAS is a token that provides temporary access to a specific resource in Azure Blob Storage. To generate a SAS, you can use the Azure portal and follow these steps:

  1. Login to the Azure portal and navigate to the storage account resource group.
  2. Click on “Shared access signature”.
  3. On the Shared access signature page, click on “Generate SAS and connection string”.

Once the SAS token is generated, it can be appended to the Azure Blob Storage URL to provide authorization credentials for AzCopy.

Creating a Container

With the SAS token in hand, we can now create a container in Azure Blob Storage using AzCopy. The syntax for creating a container is as follows:

Azcopy make "<storage_account_name>.<blob>.core.windows.net/<container_name>?<SAS_token>"

For example, to create a container named “MyFirstBLOBContainer”, execute the following command:

azcopy make "https://myazurestorage1987.blob.core.windows.net/myfirstBLOBcontainer?<SAS_token>"

Once the command is executed, you can verify that the new container has been created in the Azure portal.

Uploading a File

AzCopy allows you to upload files from your local machine to Azure Blob Storage. The syntax for uploading a file is as follows:

Azcopy copy "<Source_File>" "<storage_account_name>.<blob>.core.windows.net/<container_name>?<SAS_token>"

For example, to upload a file named “CountryRegion.csv” to the “MyFirstBLOBContainer”, execute the following command:

azcopy copy "C:\CSVFiles\CountryRegion.csv" "https://myazurestorage1987.blob.core.windows.net/myfirstblobcontainer/CountryRegion.csv?<SAS_token>"

After executing the command, you can view the uploaded file in the Azure portal.

Uploading a Directory

If you want to upload an entire directory and all its files to Azure Blob Storage, AzCopy provides a convenient way to do so. The syntax for uploading a directory is as follows:

Azcopy copy "<directory_on_local_computer>" "<storage_account_name>.<blob>.core.windows.net/<container_name>/directoryname?<SAS_token>" --recursive

For example, to upload a directory named “CSVFiles” to the “MyFirstBLOBContainer”, execute the following command:

azcopy copy "C:\CSVFiles" "https://myazurestorage1987.blob.core.windows.net/myfirstblobcontainer/CSVFiles?<SAS_token>" --recursive

After executing the command, a new directory named “CSVFiles” will be created in the “MyFirstBLOBContainer” and all the files within the directory will be uploaded.

Uploading Specific Files

If you only want to upload specific files to Azure Blob Storage, AzCopy allows you to do so by using the “–include-path” option. The syntax for uploading specific files is as follows:

Azcopy copy "<directory_on_local_computer>" "<storage_account_name>.<blob>.core.windows.net/<container_name>/directoryname?<SAS_token>" --include-path "<file1>;<file2>;<file3>"

For example, to upload three CSV files named “ProductCategory.csv”, “Shift.csv”, and “ShoppingCartItem.csv” to the “MyFirstBLOBContainer”, execute the following command:

azcopy copy "C:\CSVFiles" "https://myazurestorage1987.blob.core.windows.net/myfirstblobcontainer?<SAS_token>" --include-path "ProductCategory.csv;Shift.csv;ShoppingCartItem.csv"

After executing the command, the specified files will be uploaded to the “MyFirstBLOBContainer”.

Summary

In this article, we have explored how to use AzCopy to create containers and upload files in Azure Blob Storage. By generating a Shared Access Signature (SAS) and using AzCopy’s command-line interface, you can easily manage your data in Azure Blob Storage.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.