Mapping File Shares – Azure Storage

Once you click the file share you create, at the top you’ll see an option called Connect. Clicking Connect, Azure will display the scripts to mount or map the file share to Windows, Linux, and macOS computers, as shown in Figure 6.15:

FIGURE 6.15 Connecting to the file share

As of now, the authentication is happening using the Storage account key. If you have enabled Active Directory authentication, you can set up that as well. All you need to do is run the provided script in PowerShell, Shell, or Terminal for Windows, Linux, and macOS, respectively. To quickly demonstrate this, let’s see how you can connect to Windows and Linux computers.

Windows

To mount the file share, copy the script that you see in Figure 6.15 and open PowerShell. You don’t need to open an elevated terminal; a normal one will suffice. The following is a sample script:

$connectTestResult = Test-NetConnection -ComputerName azastorage09345.file.core.windows.net -Port 445
if ($connectTestResult.TcpTestSucceeded)
{
    # Save the password so the drive will persist on reboot
    cmd.exe /C “cmdkey /add:`”azastorage09345.file.core.windows.net`” /user:`”localhost\azastorage09345`” /pass:`”u0hG9DERY8K58QrBVKEjaYDt9bHSzsSEX1UhSQQywa6/L7IhQxlt1OyLilTA5io+q1PKIlcHapEzQmWQ6OopNA==`””
    # Mount the drive
    New-PSDrive -Name Z -PSProvider FileSystem -Root “\\azastorage09345.file.core.windows.net\webfiles” -Persist
}
else
{
    Write-Error -Message “Unable to reach the Azure storage account via port 445. Check to make sure your organization or ISP is not blocking port 445, or use Azure P2S VPN, Azure S2S VPN, or Express Route to tunnel SMB traffic over a different port.”
}

The script will automatically check if port 445 is accessible. Currently, the script will mount the file to drive letter ZI. If you already have some other drive mounted to the same drive letter, consider changing the Name parameter in New-PSDrive. In Figure 6.16, you can see that the PowerShell terminal returns that the share has been mounted successfully.

FIGURE 6.16 Mounting the file share

You can verify if the file share is working by adding a file to the mounted file share and confirm if it’s reflected in the Azure portal. In Figure 6.17, you can see that a file has been added to the drive.

Refreshing the file share will show the file you added from your local computer in the Azure portal (refer to Figure 6.18).

You can always dismount the file share from the Windows File Explorer or by using the Remove-PSDrive command from PowerShell. Now, you will mount the very same file share to a Linux machine and see if you can download the hello.jpg file.

FIGURE 6.17 Adding a file to the file share

FIGURE 6.18 Verifying files in a file share

Linux

In your Linux computer, you need to open a Terminal and run the script provided by Azure. The following is a sample script:

sudo mkdir /mnt/webfiles
if [ ! -d “/etc/smbcredentials” ]; then
sudo mkdir /etc/smbcredentials
fi
if [ ! -f “/etc/smbcredentials/azastorage09345.cred” ]; then
    sudo bash -c ‘echo “username=azastorage09345”>> /etc/smbcredentials/azastorage09345.cred’
    sudo bash -c ‘echo “password=u0hG9DERY8K58QrBVKEjaYDt9bHSzsSEX1UhSQQywa6/L7IhQxlt1OyLilTA5io+q1PKIlcHapEzQmWQ6OopNA==”>> /etc/smbcredentials/azastorage09345.cred’
fi
sudo chmod 600 /etc/smbcredentials/azastorage09345.cred
sudo bash -c ‘echo “//azastorage09345.file.core.windows.net/webfiles /mnt/webfiles cifs nofail,vers=3.0,credentials=/etc/smbcredentials/azastorage09345.cred,dir_mode=0777,file_mode=0777,serverino”>> /etc/fstab’
sudo mount -t cifs //azastorage09345.file.core.windows.net/webfiles /mnt/webfiles -o vers=3.0,credentials=/etc/smbcredentials/azastorage09345.cred,dir_mode=0777,file_mode=0777,serverino

The script will create a mount point in /mnt/webfiles, and it also updates the /etc/fstab file so that your share remains mounted even if you reboot the system. After running the script, you can see that the file share is mounted, and if you list the files in the share, hello.jpg is listed (refer to Figure 6.19). Similarly, you can mount the file share to macOS computers as well.

Since you are storing data in the file share, it’s vital to think about the recovery options in case of an accidental deletion or unintended changes. You will see how you can leverage snapshots to overcome this situation.

Leave a Reply

Your email address will not be published. Required fields are marked *