【问题标题】:Console Application to mount azure cloud drive安装 Azure 云驱动器的控制台应用程序
【发布时间】:2012-11-16 05:40:05
【问题描述】:

我想从我的私有容器挂载 vhd。在谷歌之后,我知道它只能通过.net 来实现。我更像是一个 JAVA 人。我需要一个可以在启动时自动运行并挂载 vhd 的批处理脚本或 c# 中的代码(这样我就可以获得一个 exe 文件)。所以我决定创建一个控制台应用程序来获取 exe 文件。(我对 c#/Visual Studio 的了解非常少)我正在使用以下 C# 控制台应用程序来执行此操作。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

using Microsoft.WindowsAzure.Internal;

namespace WorkerRole1
{
public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {
        // This is a sample worker implementation. Replace with your logic.
        Trace.WriteLine("WorkerRole1 entry point called", "Starting");
        MountDrive();

        //while (true)
        //{
        //    Thread.Sleep(10000);
        //    Trace.WriteLine("Working", "Information");
        //}
    }

    public override bool OnStart()
    {
        // Set the maximum number of concurrent connections 
        ServicePointManager.DefaultConnectionLimit = 12;

        // For information on handling configuration changes
        // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

        return base.OnStart();
    }

    public void MountDrive()
    {
        string connectionStringSettingName = "DefaultEndpointsProtocol=http;AccountName=abc;AccountKey=xyz";
        string azureContainerName = "vhds";
        string vhdName = "myvhd.vhd";
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionStringSettingName);
        //CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;

        LocalResource localCache = RoleEnvironment.GetLocalResource("MyAzureDriveCache");
        CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes);
        Trace.WriteLine("RootPath =====" + localCache.RootPath);
        Trace.WriteLine("MaximumSizeInMegabytes =====" + localCache.MaximumSizeInMegabytes);
        // Just checking: make sure the container exists
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        blobClient.GetContainerReference(azureContainerName).CreateIfNotExist();

        // Create cloud drive
        CloudDrive myCloudDrive = storageAccount.CreateCloudDrive(
            blobClient
            .GetContainerReference(azureContainerName)
            .GetPageBlobReference(vhdName)
            .Uri.ToString()
        );
        Trace.WriteLine("Uri =====" + blobClient
            .GetContainerReference(azureContainerName)
            .GetPageBlobReference(vhdName)
            .Uri.ToString());

        try
        {
            myCloudDrive.Create(1024);
        }
        catch (CloudDriveException ex)
        {
            // handle exception here
            // exception is also thrown if all is well but the drive already exists
        }

        string driveLetter = myCloudDrive.Mount(50, DriveMountOptions.Force);//Here It throws a Exception
        Trace.WriteLine("Drive =====" + driveLetter);

        for (int i = 0; i < 10; i++)
        {
            System.IO.File.WriteAllText(driveLetter + "\\" + i.ToString() + ".txt", "Test");
        }


    }

}
}

但我不断收到异常ERROR_DEVFABRIC_LOCAL_MOUNT_ONLY at

string driveLetter = myCloudDrive.Mount(50, DriveMountOptions.Force);

请告诉我哪里出错了?

【问题讨论】:

    标签: c# java azure azure-storage azure-clouddrive


    【解决方案1】:

    RoleEnvironment.IsAvailable 设置为 false 时,这意味着您没有在 Windows Azure Web/Worker/VM 角色中运行。 Windows Azure 驱动器仅在以这些角色挂载时才有效(因为它依赖于 RoleEnvironment)。

    更多信息可以在whitepaper找到。

    【讨论】:

    • 感谢您的快速回复。我编辑我的问题。请查看它。
    • 要在您的计算机上安装 Azure 驱动器,您需要使用开发存储:CloudStorageAccount.DevelopmentStorageAccount;
    【解决方案2】:

    ERROR_DEVFABRIC_LOCAL_MOUNT_ONLY表示本地运行时必须挂载驱动形式的开发存储。

    更改以下行:

    string connectionStringSettingName = "UseDevelopmentStorage=true";
    

    或者更好地使用RoleEnvironment.GetConfigurationSettingValue,例如:

    string connectionStringSettingName = RoleEnvironment.GetConfigurationSettingValue("DriveConnectionString");
    

    并在服务配置(文件)中设置设置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 2013-03-17
      • 1970-01-01
      相关资源
      最近更新 更多