Azure Course Labs

Azure Files Storage

Azure Files is a storage service which you can mount into your filesystem. It’s a good way to share files between components using the standard SMB protocol for network file shares.

In this lab we’ll create a file share and see how to mount it in our local machine and in an Azure VM.

Reference

Create a File Share

Azure Files is a feature of a Storage Account. Start by creating the RG and SA:

az group create -n labs-storage-files --tags courselabs=azure -l westeurope 

az storage account create -g labs-storage-files --sku Standard_LRS -n <sa-name>

Azure Files have the same performance and redundancy options as blob storage

📋 Use a storage share command to create a new file share called labs.

Not sure how?</summary>

az storage share create --help

az storage share create -n labs --account-name <sa-name>

</details>

Check the share in the Portal - you’ll see the tier and quota have used the default values.

Open the share and you’ll see you can work with files in a similar way to blobs:

  • create a new directory called uploads
  • upload the file document.txt in the lab folder to the uploads directory
  • open the file details

Click the ellipsis (...) and you can view the file contents - and edit it - directly in the Portal. You’ll see there’s a URL.

In the editor there’s a Download link; try to download the file directly from the URL:

curl -o download.txt https://<sa-name>.file.core.windows.net/labs/uploads/document.txt

cat download.txt

You’ll see an XML error string. File shares default to no public access

You can give HTTP access to a file share but you need to generate a SAS token at the account level. Typically you’ll mount the share to your local filesystem instead.

Mounting the share & rotating keys

Navigate back to the share in the Portal and click Connect. You’ll see the instructions to mount your share in Windows, macOS and Linux.

e.g. for the Mac the instructions are like this:

open smb://<sa-name>:<sa-key>@<sa-name>.file.core.windows.net/<share-name>

Mount the share on your local machine. Confirm you can see the document.txt and edit the contents - open it again in the Portal to check your changes are there. Make a change in the Portal and confirm you see it in your local share.

You may see interesting messages about the file sytsem capabilities - SMB doesn’t have all the features of your native OS filesystem.

Authentication to a share uses the storage account key - this is autogenerated when the account is created. It gives you access to the whole storage account.

Check the SA in the Portal, open Access keys. You’ll see key1 and key2 both with options to rotate them. If you share your keys you should rotate them regularly - clients will need to know the new key.

az storage account keys list --account-name <sa-name>

You’ll see the value for key1 is the one you used to mount the share. Renew the key and the access key will be replaced with a new one:

az storage account keys renew --key primary -g labs-storage-files -n <sa-name> 

Now try to open the file from your local share again. It will fail, maybe with an error message - depending on your OS. Renewing the key invalidates autheication with the old key. You need to connect again with the new key.

Mount the share in a VM

Mounting the share is the same process in a VM. You can capture that in a script to run when a new VM is created, so it has access to the share straight away.

This script is for a Linux VM - it will need to be updated with your details:

When you’ve edited the script, create a VM using cloud-init to run the script and mount the share:

az vm create -g labs-storage-files -n vm01 --image UbuntuLTS --custom-data @labs/storage-files/cloud-init/mount-share.sh

Connect to the VM and check you can read and edit the file:

ssh <ip-address>

ls /mnt/labs

cat /mnt/labs/uploads/document.txt

echo 'EDITED once more by Azure VM.' >> /mnt/labs/uploads/document.txt

exit

Verify the changes in the Portal. All connected clients see the same data.

Lab

File shares have a set capacity for how much they can store. When they’re full clients will get an error if they try to write more data. Can you increase the capacity of your existing share?

Azure Files also supports a premium tier which uses fast solid-state disks. Create a premium share with 100GB capacity. What’s different about the premium tier?

Stuck? Try hints or check the solution.


Cleanup

Delete the lab RG:

az group delete -y -n labs-storage-files --no-wait