Azure Files is a fully managed cloud-based file share service that allows you to easily store and share files in the cloud. In this tutorial, we will walk you through the steps to create and manage Azure Files using the Azure CLI commands.

Prerequisites

  • An Azure account with an active subscription
  • Azure CLI installed on your local machine
  • Basic knowledge of Azure storage accounts and Azure CLI

Step 1: Create a Resource Group

The first step is to create a resource group to host our Azure Files. You can use the following Azure CLI command to create a resource group:

az group create --name myResourceGroup --location eastus

Replace myResourceGroup with a name of your choice and eastus with the region where you want to create the resource group.

Step 2: Create a Storage Account

Next, create a storage account in the resource group that we created in the previous step. You can use the following Azure CLI command to create a storage account:

az storage account create --name mystorageaccount --location eastus --resource-group myResourceGroup --sku Standard_LRS --kind StorageV2

Replace mystorageaccount with a name of your choice and eastus with the region where you want to create the storage account. Also, make sure to replace myResourceGroup with the name of the resource group that you created in the previous step.

Step 3: Create an Azure File Share

Now that we have a storage account, we can create an Azure file share. You can use the following Azure CLI command to create an Azure file share:

az storage share create --name myfileshare --account-name mystorageaccount --account-key $(az storage account keys list --account-name mystorageaccount --resource-group myResourceGroup --query "[0].value" -o tsv)

Replace myfileshare with a name of your choice and mystorageaccount with the name of the storage account that you created in the previous step. Also, make sure to replace myResourceGroup with the name of the resource group that you created in step 1.

Note that we are using the Azure CLI command az storage account keys list to retrieve the storage account key, which is required to authenticate the Azure file share creation.

Step 4: Mount the Azure File Share

To access the Azure file share, you can mount it as a network drive on your local machine. You can use the following Azure CLI command to get the connection string for the Azure file share:

az storage account show-connection-string --name mystorageaccount --resource-group myResourceGroup --query "connectionString" --output tsv

This command will output the connection string for the storage account that we created in step 2. Copy the connection string and replace <connection-string> in the following command with the copied connection string:

sudo mount -t cifs //<connection-string>/myfileshare /mnt/myfileshare -o vers=3.0,username=<storage-account-name>,password=<storage-account-key>,dir_mode=0777,file_mode=0777

This command will mount the Azure file share as a network drive on the /mnt/myfileshare directory on your local machine. Replace <connection-string> with the connection string that you copied in the previous step, <storage-account-name> with the name of the storage account that you created in step 2, and <storage-account-key> with the storage account key that you retrieved in step 3.

Step 5: Upload and Manage Files

Now that we have mounted the Azure file share on our local machine, we can upload and manage files in the file share. Here are some common Azure CLI commands to manage Azure Files:

Upload a file to the Azure file share

You can use the following Azure CLI command to upload a file to the Azure file share:

az storage file upload-batch --destination myfileshare --source /path/to/local/folder --account-name mystorageaccount --account-key $(az storage account keys list --account-name mystorageaccount --resource-group myResourceGroup --query "[0].value" -o tsv)

Replace /path/to/local/folder with the path to the folder containing the file that you want to upload. Also, make sure to replace mystorageaccount with the name of the storage account that you created in step 2 and myResourceGroup with the name of the resource group that you created in step 1.

List files in the Azure file share

You can use the following Azure CLI command to list the files in the Azure file share:

az storage file list --share-name myfileshare --account-name mystorageaccount --account-key $(az storage account keys list --account-name mystorageaccount --resource-group myResourceGroup --query "[0].value" -o tsv)

This command will output a list of files in the Azure file share.

Download a file from the Azure file share

You can use the following Azure CLI command to download a file from the Azure file share:

az storage file download --share-name myfileshare --path <file-path-in-share> --dest /path/to/local/folder --account-name mystorageaccount --account-key $(az storage account keys list --account-name mystorageaccount --resource-group myResourceGroup --query "[0].value" -o tsv)

Replace <file-path-in-share> with the path to the file in the Azure file share that you want to download. Also, make sure to replace mystorageaccount with the name of the storage account that you created in step 2 and myResourceGroup with the name of the resource group that you created in step 1.

Delete a file from the Azure file share

You can use the following Azure CLI command to delete a file from the Azure file share:

az storage file delete --share-name myfileshare --path <file-path-in-share> --account-name mystorageaccount --account-key $(az storage account keys list --account-name mystorageaccount --resource-group myResourceGroup --query "[0].value" -o tsv)

Replace <file-path-in-share> with the path to the file in the Azure file share that you want to delete. Also, make sure to replace mystorageaccount with the name of the storage account that you created in step 2 and myResourceGroup with the name of the resource group that you created in step 1.

Conclusion

In this tutorial, we walked you through the steps to create and manage Azure Files using Azure CLI commands. With Azure Files, you can easily store and share files in the cloud with the help of Azure CLI commands.