如何使用Rest Api创建azure VM

时间:2013-06-03 02:06:44

标签: azure

这是代码,但它会提示错误:

  

图片o3lceiy3.ioa201305211013360129.vhd不存在。

subscriptionIdX509Certificate2有效

    internal class Program
      {
        public static X509Certificate2 Certificate { get; set; }

        private static void Main(string[] args)
         {
          const string subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

                             //@"https://management.core.windows.net/<subscription-id>/services/hostedservices/<cloudservice-name>/deployments";
           var url = string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments",
                            subscriptionId, "edoc2cloudtest");
          var myReq = (HttpWebRequest)WebRequest.Create(url);
          myReq.Method = "POST";
          myReq.Headers.Add("x-ms-version", "2012-03-01");
          myReq.Proxy = null;
          myReq.Timeout = 30000;
          myReq.ContentType = "application/xml";
          var postData = ReadConfig();
          using (var reqStream = myReq.GetRequestStream())
          {
            var data = Encoding.UTF8.GetBytes(postData);
            reqStream.Write(data, 0, data.Length);
            reqStream.Flush();
          }
            Certificate = GetX509Certificate();
            myReq.ClientCertificates.Add(Certificate);
          try
          {
            var myRes = (HttpWebResponse) myReq.GetResponse();
          }
          catch (WebException exWeb)
          {
            // Parse the web response.
            Stream responseStream = exWeb.Response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            XmlDocument xDocResp = new XmlDocument();
            xDocResp.Load(reader);

            HttpWebResponse responseStatus = (HttpWebResponse)exWeb.Response;

            responseStream.Close();
            reader.Close();

            var result = NiceFormatXml(xDocResp);

            Console.WriteLine(result);
          }

      }

    private static string NiceFormatXml(XmlDocument xDoc)
    {
        StringBuilder niceString = new StringBuilder();

        StringWriter strWriter = new StringWriter(niceString);

        XmlTextWriter xmlWriter = new XmlTextWriter(strWriter);

        xmlWriter.Formatting = Formatting.Indented;

        xDoc.WriteTo(xmlWriter);

        xmlWriter.Close();
        strWriter.Close();

        return niceString.ToString();
    }

    private static X509Certificate2 GetX509Certificate()
    {
        X509Certificate2 certificate2 = null;
        var store = new X509Store("MY", StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        var collection = store.Certificates;
        var fcollection = collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
        const string certificateThumbprint = "‎‎‎‎7dfbc7369306ed096b7e5c7b4ba6e99f190240e9";
        store.Close();
        if (fcollection.Count > 0)
        {
            foreach (var variable in fcollection)
            {
                if (variable.Thumbprint != null &&
                    variable.Thumbprint.Equals(certificateThumbprint, StringComparison.InvariantCultureIgnoreCase))
                {
                    certificate2 = variable;
                }
            }
        }
        return certificate2;
    }

    private static string ReadConfig()
    {
        string path = System.AppDomain.CurrentDomain.BaseDirectory + "Edoc2Cloud.xml";
        //string path = System.AppDomain.CurrentDomain.BaseDirectory + "VM-CreateVM.xml";
        string s;
        using (var sr = new StreamReader(path, Encoding.GetEncoding("GB2312")))
        {
            s = sr.ReadToEnd();
        }
        return s;
    }
}

这是XML:

    <Deployment xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Name>EDoc2Test</Name>
    <DeploymentSlot>Staging</DeploymentSlot>
    <Label>EDoc2Testlabe</Label>      
    <RoleList>
    <Role>
    <RoleName>EDoc2TestRoleName</RoleName>
    <RoleType>PersistentVMRole</RoleType>   
    <ConfigurationSets>  
      <ConfigurationSet>        
             <ConfigurationSetType>WindowsProvisioningConfiguration</ConfigurationSetType>
        <ComputerName>computer-name</ComputerName>
        <AdminPassword>APasswor_324d</AdminPassword> 
        <EnableAutomaticUpdates>true</EnableAutomaticUpdates>  
      </ConfigurationSet>
     </ConfigurationSets>
  <AvailabilitySetName>EDoc2TestSetName</AvailabilitySetName>
    <OSVirtualHardDisk>
     <HostCaching>ReadWrite</HostCaching>    
    <DiskName>SomeName-0-20121007173943</DiskName>
    <MediaLink>http://portalvhdsx4flx9dhmjyt1.blob.core.windows.net/vhds/o3lceiy3.ioa201305211013360129.vhd</MediaLink>
    <SourceImageName>o3lceiy3.ioa201305211013360129.vhd</SourceImageName>
  </OSVirtualHardDisk>    
  <RoleSize>Medium</RoleSize>      
</Role>

enter image description here

1 个答案:

答案 0 :(得分:0)

根据您收到的错误以及您指定的XML,是否可以检查自定义图片中是否有名为o3lceiy3.ioa201305211013360129.vhd的图片?您可以通过登录门户并转到虚拟机来查找该信息 - &gt;图像。

enter image description here

有关<SourceImageName>参数的文档说明当您要使用系统或自定义映像创建虚拟机时需要它。

您可以在此处阅读完整的文档:http://msdn.microsoft.com/en-us/library/windowsazure/jj157186.aspx#OSVirtualHardDisk