Azure file storage, writing file blobs to a drive

Azure storage is one of those things you use every day, without knowing it. For example, Azure functions Apps uses it; in fact, all kinds of (Azure) App uses it as storage And if we are aware of using it, it is mostly about Blob storage, table storage or queues.

For more than two years ago, Azure File storage was introduced. At first, it looks and feels like regular blob storage but there is a little difference:  a blob container is called a share. To be more specific, a share can actually be shared using the SMB protocol. So a drive letter can be assigned to the file storage share.

Let’s see how this works. If you do not have an Azure storage available, please create one. Here are the four kinds of storage in Azure storage.

fs01

 

First, we create a new file storage with a share and a directory. Then we write a file in it. After that, we try to consume the share.

Creating a file storage share

Let’s add a new file share. Select the Files panel in the Azure Storage and add a file share:

fs02

You will have to give the share a unique name (use lower case) and it has to have a certain quota. The size can be as large 5120GB. Although it seems strange to give Azure storage a fixed size, this share will behave as a drive. Having a maximum size is good:

fs03

Of course, you can always add an extra drive (create another share):

fs04

So the share is created, click on it and see the options:

fs05

For now, we first add a new directory by selecting Add directory:

fs11

We give the directory a unique name ‘CustomLogs’:

fs12

And now the directory is created:

fs13

We could upload an existing file but instead, we create one by coding in Visual Studio.

Creating a file using C#

Open Visual Studio and add a new console app. Reference an extra NuGet package name “windowsazure.storage”. Pick the one with 7 million downloads. This will introduce several other NuGet packages:

fs10

Accept them all.

Replace the Main method with this code:

private static void Main(string[] args)
{
    // file storage
     var shareName = "testfilestorage";
     var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["Storage"].ToString());
     var fileClient = storageAccount.CreateCloudFileClient();
 
    // Get a reference to the file share we created previously.
    var share = fileClient.GetShareReference(shareName);
 
    // Ensure that the share exists.
    if (share.Exists())
    {
        Console.WriteLine("File share found");
 
        // Get a reference to the root directory for the share.
        var rootDir = share.GetRootDirectoryReference();
 
        var sampleDir = rootDir.GetDirectoryReference("CustomLogs");
 
        // Ensure that the directory exists.
        if (sampleDir.Exists())
        {
            Console.WriteLine("File dir found");
 
            // Get a reference to the file we created previously.
            CloudFile file = sampleDir.GetFileReference("Log1.txt");
 
            // Ensure that the file exists.
            if (file.Exists())
            {
                Console.WriteLine("File found");
 
                // Write the contents of the file to the console window.
                Console.WriteLine(file.DownloadTextAsync().Result);
            }
            else
            {
                Console.WriteLine("File not found");
            }
 
            // create file
 
            var fileToCreate = sampleDir.GetFileReference("outputgenerated.txt");
 
            fileToCreate.UploadText("Yadda");

            Console.WriteLine("File created");
        }
        else
        {
            Console.WriteLine("File dir not found");
        }
    }
    else
    {
        Console.WriteLine("File share not found");
    }

    Console.WriteLine("Press a key");
    Console.ReadKey();</pre>

First, the existence of the share and the directory are checked. Then we check if the file to create, already exists. If not, it will be created.

At last, you need the following Application setting. This is the connection string for the Azure Storage. It’s just the combination of the name and one of the API keys:

<appSettings>
    <add key="Storage" value="DefaultEndpointsProtocol=https;AccountName=cgteststoraccount;AccountKey=[Add one of the API keys]" />
  </appSettings>

To get the name and key, go to the Azure Store settings. Go to the Access keys:

fs08

There you can find the name of the storage and both keys. Pick one key and fill it in, into the app setting:

fs09

Run the code and see a file is created:

fs14

Go to the Azure portal and check the availability of the file:

fs15

Create a Windows 10 VM

Our share is up and running, a directory and a file are available. So now comes the fun part, let’s check if this share can be reached using a VM in Azure.

Withing the same resource group of your Azure Storage, create a new Virtual machine. Click Add:

fs16

Search for a Windows 10 operating system:

fs17

And create a VM based on that template. VM’s are not really cheap (for a hobbyist), but we only need it for a short period of time:

fs18

Creating a VM takes a couple of minutes, let’s check something else first…

How to connect, using a drive letter

Remember, the share can be made available as a drive. How should it be done? For that, within the Azure portal click the connect button on the Share panel:

fs06

This will not actually connect but it gives an explanation:

fs07

So we need to execute something like (copy the text from the panel):

net use [drive letter] \\cgteststoraccount.file.core.windows.net\testfilestorage /u:cgteststoraccount [storage account access key]

And we can connect using Linux too. That’s great!

There is some fine print about “TCP port 445”. It should be possible to connect from outside Azure too, nice 🙂

Adding the share

So make an RDP connection to the newly created VM in Azure. When it is created, Open a file explorer en see that drive F is not yet taken:

fs19

So open a dos box (command prompt) and paste the complete “net use” sentence (with drive F entered):

fs20

Note: A drive letter is written as “F:”

You can check the creation using “net use”. But you can also open a file explorer:

fs21

Yes, the file is shown. As the owner of the VM you can now read, insert, delete, and update files, just like a regular drive.

But the connection to your F drive is a temporary one. If you reboot the machine, the “Net Use” connection is lost, on booting the password is not available. But be not afraid, the share and your files are still there.

My solution is to drop the connection first:

fs22

And then you just need to connect again.

Map a drive like a Pro

There is another way to map a drive which will be remembered after a sign-in.

Go to the Windows Explorerer and select “Map network drive”:

fs-ext02

Fill in the address of the File Storage and check the Reconnect question:

fs-ext03

After that, you have to provide the username and password. The username is “cgteststoraccount” and the API key is the password. Put a ‘\’ in front of the username so it is not part of any domain:

fs-ext04

Check the “Remember my credentials” and select OK.

Now a drive is mapped and it will be remembered

Conclusion

Azure File storage connects cheap redundant azure storage with VM’s or even your own machine. Great!

Advertenties