Search code examples
azure-storagevirtual-directorysmbazure-cloud-servicescloud-storage

How to run NET USE to work to map a drive in Cloud Service to an Azure File Services share within webrole


I have created a preview storage account and requested access to the Azure File Services share which allows you to create a share that can be accessed across instances. I have found the following blog post provided by the storage team:

http://blogs.msdn.com/b/windowsazurestorage/archive/2014/05/27/persisting-connections-to-microsoft-azure-files.aspx

I have successfully created the share programmatically within my web role start using the following code:

public static void CreateCloudShare()
{
    CloudStorageAccount account = CloudStorageAccount.Parse(System.Configuration.ConfigurationManager.AppSettings["SecondaryStorageConnectionString"].ToString());
    CloudFileClient client = account.CreateCloudFileClient();
    CloudFileShare share = client.GetShareReference("scorm");
    share.CreateIfNotExistsAsync().Wait();
}

If I log onto the server I can successfully map the drive using the following code from the cmd.exe window:

D:\>net use z: \\<storage-account>.file.core.windows.net\scorm /u:<storage-account> <storage-password>

I have attempted to include the following code in my Startup.cmd

use z: \\<storage-account>.file.core.windows.net\scorm /u:<storage-account> <storage-password>

If I log onto the new instance then I can see that there is a z: drive present but says that it is disconnected with the error or password is incorrect error


Solution

  • As also mentioned in the blog post, Windows maintains different contexts for each user. Each context can be independently connected to a different set of SMB shares, and each context will have its own drive letter mapping to the shares it is connected to.

    Please refer to "User Contexts" and "Web Roles and User Contexts" sections of the same blog post. If your goal is to make sure the drive is accessible from your web role, calling WNetAddConnection2 from Global.Application_Start is a good solution.