【问题标题】:Mapping an Azure File Service CloudFileShare as a virtual directory on each instance of a cloud service将 Azure 文件服务 CloudFileShare 映射为每个云服务实例上的虚拟目录
【发布时间】:2014-07-25 16:18:50
【问题描述】:

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

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 进行映射,但我不确定如何在我的网络角色中调用代码。我添加了以下方法来运行 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();
}

【问题讨论】:

    标签: powershell azure-storage smb net-use


    【解决方案1】:

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

    【讨论】:

    • 我在哪里放置 WNetAddConnection2 代码?这是否在 global.asax 中?
    • 正确,它将在全球范围内。
    • Brilliant 成功了,非常感谢您的优秀博客 Serdar 非常感谢我遇到了一些问题,即在资源管理器中未清理断开的驱动器。请注意,当您确实为 PaaS 工作时,它不会出现在远程用户的资源管理器中的映射列表中
    【解决方案2】:

    RedDog.Storage 库使在您的云服务中安装驱动器变得非常容易,而无需担心 P/Invoke:

    Install-Package RedDog.Storage
    

    安装包后,您可以简单地在您的 CloudFileShare 上使用扩展方法“Mount”:

    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/

    【讨论】:

    • 不幸的是,我从网络作业控制台收到了“ERROR_ACCESS_DENIED”。有什么解决方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多