如何使用RESTAPI创建azure VM
以下是代码,但它会提示错误: 映像o3lceiy3.ioa201305211013360129.vhd不存在如何使用RESTAPI创建azure VM,azure,Azure,以下是代码,但它会提示错误: 映像o3lceiy3.ioa201305211013360129.vhd不存在 subscriptionId和X509Certificate2有效 internal class Program { public static X509Certificate2 Certificate { get; set; } private static void Main(string[] args) {
subscriptionId
和X509Certificate2
有效
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;
}
}
内部类程序
{
公共静态X509Certificate2证书{get;set;}
私有静态void Main(字符串[]args)
{
常量字符串subscriptionId=“xxxxxxxx-xxxx-xxxx-xxxx-XXXXXXXXXX”;
//@"https://management.core.windows.net//services/hostedservices//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=“应用程序/xml”;
var postData=ReadConfig();
使用(var reqStream=myReq.GetRequestStream())
{
var data=Encoding.UTF8.GetBytes(postData);
请求流写入(数据,0,数据长度);
reqStream.Flush();
}
Certificate=GetX509Certificate();
myReq.ClientCertificates.Add(证书);
尝试
{
var myRes=(HttpWebResponse)myReq.GetResponse();
}
捕获(WebException exWeb)
{
//解析web响应。
Stream responseStream=exWeb.Response.GetResponseStream();
StreamReader=新的StreamReader(responseStream);
XmlDocument xDocResp=新的XmlDocument();
xDocResp.Load(读卡器);
HttpWebResponse responseStatus=(HttpWebResponse)exWeb.Response;
responseStream.Close();
reader.Close();
var result=NiceFormatXml(xDocResp);
控制台写入线(结果);
}
}
私有静态字符串XML(XmlDocument xDoc)
{
StringBuilder niceString=新的StringBuilder();
StringWriter strWriter=新的StringWriter(字符串);
XmlTextWriter xmlWriter=新的XmlTextWriter(strWriter);
xmlWriter.Formatting=Formatting.Indented;
xDoc.WriteTo(xmlWriter);
xmlWriter.Close();
strWriter.Close();
返回niceString.ToString();
}
私有静态X509Certificate2 GetX509Certificate()
{
X509Certificate2 certificate2=null;
var store=new X509Store(“我的”,StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
var collection=store.Certificates;
var fcollection=collection.Find(X509FindType.FindByTimeValid,DateTime.Now,false);
常量字符串certificateThumbprint=”7dfbc7369306ed096b7e5c7b4ba6e99f190240e9”;
store.Close();
如果(fcollection.Count>0)
{
foreach(fcollection中的var变量)
{
if(variable.Thumbprint!=null&&
variable.Thumbprint.Equals(certificateThumbprint、StringComparison.InvariantCultureInogoreCase))
{
证书2=变量;
}
}
}
返回证书2;
}
私有静态字符串ReadConfig()
{
字符串路径=System.AppDomain.CurrentDomain.BaseDirectory+“Edoc2Cloud.xml”;
//字符串路径=System.AppDomain.CurrentDomain.BaseDirectory+“VM CreateVM.xml”;
字符串s;
使用(var sr=newstreamreader(路径,Encoding.GetEncoding(“GB2312”))
{
s=sr.ReadToEnd();
}
返回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>
EDOC2试验
登台
EDoc2Testlabe
EDoc2TestRoleName
PersistentVMRole
窗口配置
计算机名
阿帕斯沃鲁324d酒店
真的
EDoc2TestSetName
读写
SomeName-0-20121007173943
http://portalvhdsx4flx9dhmjyt1.blob.core.windows.net/vhds/o3lceiy3.ioa201305211013360129.vhd
o3lceiy3.IOA2013052110110360129.vhd
中等
根据您收到的错误和您指定的XML,您是否可以检查您的自定义图像中是否有名为
o3lceiy3.IOA2013052111013360129.vhd
的图像?您可以通过登录门户并转到虚拟机-->映像来查找这些信息
有关
参数的文档说明,当您希望使用系统映像或自定义映像创建虚拟机时,需要该参数
您可以在此处阅读完整的文档:。帮助您屏蔽订阅ID。您也可以发布XML请求正文(发布数据)吗?我有一个图像,状态正常(我上传了上面的一张图片)。我捕获了一个图像并从该图像创建了一个vm,但还需要几个小时才能完成,我该怎么办?你是什么意思?
它花了几个小时还没有完成。我建议创建一个新的帖子,而不是继续在这里。