Windows Azure REST API MediaLink

标签 windows api rest azure

我正在尝试使用 Windows Azure 的 API REST 来创建虚拟机部署。但是,当我尝试在以下 XML 文件中指定操作系统镜像时遇到问题:

<Deployment xmlns="http://schemas.microsoft.com/windowsazure"   xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Name>SomeName</Name>
  <DeploymentSlot>Production</DeploymentSlot>
  <Label></Label>
  <RoleList>
  <Role i:type="PersistentVMRole">
      <RoleName>SomeName</RoleName>
      <OsVersion i:nil="true"/>
      <RoleType>PersistentVMRole</RoleType>
      <ConfigurationSets>
        <ConfigurationSet i:type="WindowsProvisioningConfigurationSet">
        <ConfigurationSetType>WindowsProvisioningConfiguration</ConfigurationSetType>
        <ComputerName>SomeName</ComputerName>
        <AdminPassword>XXXXXXXXXX</AdminPassword>
        <EnableAutomaticUpdates>true</EnableAutomaticUpdates>
        <ResetPasswordOnFirstLogon>false</ResetPasswordOnFirstLogon>
      </ConfigurationSet>
      <ConfigurationSet i:type="NetworkConfigurationSet">
         <ConfigurationSetType>NetworkConfiguration</ConfigurationSetType>
         <InputEndpoints>
         <InputEndpoint>
             <LocalPort>3389</LocalPort>
             <Name>RemoteDesktop</Name>
             <Protocol>tcp</Protocol>
         </InputEndpoint>
         </InputEndpoints>
      </ConfigurationSet>
      </ConfigurationSets>
      <DataVirtualHardDisks/>
      <Label></Label>
      <OSVirtualHardDisk>
      <MediaLink>¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿???????????????</MediaLink>
      <SourceImageName>¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿??????????????????</SourceImageName>
      </OSVirtualHardDisk>
    </Role>
  </RoleList>
</Deployment>`

我需要 MediaLink(操作系统镜像的 URI)和 SourceImageName(操作系统镜像的规范名称)。我的问题是,门户网站提供了几个预定义图像,但我无法确定它们的 URI 和规范名称。我是否会被迫创建自己的操作系统镜像并将其上传到我的 Windows Azure 帐户下的任何存储服务?

最佳答案

要获取这些参数,您可以执行 List OS Images 您的订阅上的服务管理 API 操作。

更新 请丢弃我下面的一些评论(对此表示抱歉)。我终于能够使用 REST API 创建虚拟机了:)。以下是一些事情:

  1. <MediaLink>元素应指定将在其上创建 VM 的 VHD 的 URL。它必须是与虚拟机云服务处于同一订阅中的存储帐户之一中的 URL。因此,为此,您可以指定如下 URL: https://[yourstorageaccount].blob.core.windows.net/[blobcontainer]/[filename].vhd,其中 [blobcontainer] 是您所在的 Blob 容器的名称希望 API 存储 VHD,而 [文件名] 是您想要为 VHD 指定的任何名称。 REST API 的作用是复制 <SourceImageName> 中指定的源图像。并将其保存在 <MediaLink> 中指定的 URI 处元素。
  2. 确保将存储 VHD 的服务帐户和存储帐户位于同一数据中心/关联组中。此外,该数据中心应该能够支持虚拟机。事实证明,并非所有数据中心都支持虚拟机。
  3. XML 元素的顺序至关重要。向上或向下移动一个元素将导致 400 错误。

根据我的实验,代码如下:

        private static void CreateVirtualMachineDeployment(string subscriptionId, X509Certificate2 cert, string cloudServiceName)
        {
            try
            {
                string uri = string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments", subscriptionId, cloudServiceName);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "POST";
                request.ContentType = "application/xml";
                request.Headers.Add("x-ms-version", "2013-03-01");
                request.ClientCertificates.Add(cert);
                string requestPayload = @"<Deployment xmlns=""http://schemas.microsoft.com/windowsazure"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
  <Name>[SomeName]</Name>
  <DeploymentSlot>Production</DeploymentSlot>
  <Label>[SomeLabel]</Label>
  <RoleList>
    <Role i:type=""PersistentVMRole"">
      <RoleName>MyTestRole</RoleName>
      <OsVersion i:nil=""true""/>
      <RoleType>PersistentVMRole</RoleType>
      <ConfigurationSets>
        <ConfigurationSet i:type=""WindowsProvisioningConfigurationSet"">
          <ConfigurationSetType>WindowsProvisioningConfiguration</ConfigurationSetType>
          <ComputerName>[ComputerName]</ComputerName>
          <AdminPassword>[AdminPassword - Ensure it's strong Password]</AdminPassword>
          <AdminUsername>[Admin Username]</AdminUsername>
          <EnableAutomaticUpdates>true</EnableAutomaticUpdates>
          <ResetPasswordOnFirstLogon>false</ResetPasswordOnFirstLogon>
        </ConfigurationSet>
        <ConfigurationSet i:type=""NetworkConfigurationSet"">
          <ConfigurationSetType>NetworkConfiguration</ConfigurationSetType>
          <InputEndpoints>
            <InputEndpoint>
              <LocalPort>3389</LocalPort>
              <Name>RemoteDesktop</Name>
              <Protocol>tcp</Protocol>
            </InputEndpoint>
          </InputEndpoints>
        </ConfigurationSet>
      </ConfigurationSets>
      <DataVirtualHardDisks/>
      <Label></Label>
      <OSVirtualHardDisk>
        <MediaLink>https://[storageaccount].blob.core.windows.net/vhds/fb83b3509582419d99629ce476bcb5c8__Microsoft-SQL-Server-2012SP1-Web-CY13SU04-SQL11-SP1-CU3-11.0.3350.0.vhd</MediaLink>
        <SourceImageName>fb83b3509582419d99629ce476bcb5c8__Microsoft-SQL-Server-2012SP1-Web-CY13SU04-SQL11-SP1-CU3-11.0.3350.0</SourceImageName>
      </OSVirtualHardDisk>
    </Role>
  </RoleList>
</Deployment>";
                byte[] content = Encoding.UTF8.GetBytes(requestPayload);
                request.ContentLength = content.Length;
                using (var requestStream = request.GetRequestStream())
                {
                    requestStream.Write(content, 0, content.Length);
                }

                using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
                {
                }
            }
            catch (WebException webEx)
            {
                using (var streamReader = new StreamReader(webEx.Response.GetResponseStream()))
                {
                    string result = streamReader.ReadToEnd();
                    Console.WriteLine(result);
                }
            }

        }

希望这有帮助!

关于Windows Azure REST API MediaLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16584052/

相关文章:

rest - 什么是 REST API 的组件?

Python ctypes 无法在结构中传递内存数组

android - 在 Windows 10 上使用 Android Studio 设置向导运行英特尔® HAXM 安装程序需要很长时间

c# - 是否可以在 Windows 7 和 Visual Studio 2012 中开发 Windows Mobile 应用程序

javascript - express promise 链错误

rest - 如何使用基于 wink 的 REST API 对 dojox.grid.DataGrid 进行排序?

windows - 设置代理服务器的步骤

javascript - Google Plus API - key 无效

node.js - 如何在 Node Js API 中添加身份验证?

java - Java maven spring 框架的 Restful API 不工作