将Azure文件服务CloudFileShare映射为云服务的每个实例上的虚拟目录

时间:2014-07-25 16:18:51

标签: powershell azure-storage smb net-use

我有一个azure云服务,我正在尝试升级以获得高可用性,并且我订阅了已在预览门户中启用的Microsoft Azure文件服务预览。我创建了一个新的存储帐户,可以看到存储帐户现在有一个位于以下位置的文件端点:

https://<account-name>.file.core.windows.net/

在我的网络角色中,我有以下代码,查看是否创建了一个名为scorm的共享,如果没有,则创建它:

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();
}

这没有问题。我的问题是,我不确定如何映射已在云服务中创建为虚拟目录的CloudShare。在一个实例上,我能够做到这一点:

public static void CreateVirtualDirectory(string VDirName, string physicalPath)
{
    try
    {

        if (VDirName[0] != '/')
            VDirName = "/" + VDirName;

        using (var serverManager = new ServerManager())
        {
            string siteName = RoleEnvironment.CurrentRoleInstance.Id + "_" + "Web";
            //Site theSite = serverManager.Sites[siteName];
            Site theSite = serverManager.Sites[0];
            foreach (var app in theSite.Applications)
            {
                if (app.Path == VDirName)
                {
                    // already exists
                    return;
                }
            }
            Microsoft.Web.Administration.VirtualDirectory vDir = theSite.Applications[0].VirtualDirectories.Add(VDirName, physicalPath);
            serverManager.CommitChanges();
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.EventLog.WriteEntry("Application", ex.Message, System.Diagnostics.EventLogEntryType.Error);
        //System.Diagnostics.EventLog.WriteEntry("Application", ex.InnerException.Message, System.Diagnostics.EventLogEntryType.Error);
    }
}

我已经看过并看到可以通过powershell映射这个,但我不确定如何在我的web角色中调用代码。我添加了以下方法来运行powershell代码:

public static int ExecuteCommand(string exe, string arguments, out string error, int timeout)
{
    Process p = new Process();
    int exitCode;
    p.StartInfo.FileName = exe;
    p.StartInfo.Arguments = arguments;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardError = true;
    p.Start();
    error = p.StandardError.ReadToEnd();
    p.WaitForExit(timeout);
    exitCode = p.ExitCode;
    p.Close();

    return exitCode;
}

我知道我必须运行的命令是:

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

如何在我的网络角色中使用此功能?我的网络角色代码如下,但似乎无法正常工作:

public override bool OnStart()
{
    try
    {

        CreateCloudShare();
        ExecuteCommand("net.exe", "user " + userName + " " + password + " /add", out error, 10000);
        ExecuteCommand("netsh.exe", "firewall set service type=fileandprint mode=enable scope=all", out error, 10000);
        ExecuteCommand("net.exe", " share " + shareName + "=" + path + " /Grant:" + userName + ",full", out error, 10000);

    }
    catch (Exception ex)
    {
        System.Diagnostics.EventLog.WriteEntry("Application", "CREATE CLOUD SHARE ERROR : " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
    }
    return base.OnStart();
}

2 个答案:

答案 0 :(得分:2)

我们的博客文章Persisting connections to Microsoft Azure Files提供了一个从Web和辅助角色引用Azure Files共享的示例。请参阅“Windows PaaS角色”部分,并查看“Web角色和用户上下文”下的说明。

答案 1 :(得分:1)

库RedDog.Storage可以很容易地在您的Cloud Service中安装驱动器,而不必担心P / Invoke:

Install-Package RedDog.Storage

安装软件包后,您只需使用扩展方法&#34; Mount&#34;在您的CloudFileShare上:

public class WebRole : RoleEntryPoint
{
    public override bool OnStart()
    {
        // Mount a drive.
        FilesMappedDrive.Mount("P:", @"\\acc.file.core.windows.net\reports", "sandibox", 
            "key");

        // Unmount a drive.
        FilesMappedDrive.Unmount("P:");

        // Mount a drive for a CloudFileShare.
        CloudFileShare share = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"))
            .CreateCloudFileClient()
            .GetShareReference("reports");
        share.Mount("P:");

        // List drives mapped to an Azure Files share.
        foreach (var mappedDrive in FilesMappedDrive.GetMountedShares())
        {
            Trace.WriteLine(String.Format("{0} - {1}", mappedDrive.DriveLetter, mappedDrive.Path));
        }

        return base.OnStart();
    }
}

更多信息:http://fabriccontroller.net/blog/posts/using-the-azure-file-service-in-your-cloud-services-web-roles-and-worker-role/