不是为了打败死马,而是为了我的任务,我必须在 HTML 表中打印出实例 ID 和实例类型。如果您想在网站上托管它,这可能很有用。您需要做的就是更改StreamWriter
. 这是我草率但成品。
AmazonEC2 ec2 = new AmazonEC2Client();
DescribeInstancesRequest req = new DescribeInstancesRequest();
List<Amazon.EC2.Model.Reservation> result = ec2.DescribeInstances(req).DescribeInstancesResult.Reservation;
using (StreamWriter writer = new StreamWriter(@"C:\Users\Ethan\Desktop\InstanceOutput.html")) //change this output to a local address if testing the code
{
writer.Write("<table border=1><tbody><tr><th>Instance ID</th><th>Instance Type</th></tr>");
foreach (Amazon.EC2.Model.Reservation reservation in result)
{
foreach (Amazon.EC2.Model.RunningInstance runningInstance in reservation.RunningInstance)
{
writer.WriteLine("<tr><td>" + runningInstance.InstanceId + "</td><td>" + runningInstance.InstanceType + "</td></tr>");
}
}
writer.Write("</tbody></table>");
}
所以请记住,我对此是全新的,有人可能会想出一个更清洁、更有效和更好的答案。这就是我在控制台中打印实例 ID 和实例类型的方式。
AmazonEC2 ec2 = new AmazonEC2Client();
DescribeInstancesRequest req = new DescribeInstancesRequest();
List<Amazon.EC2.Model.Reservation> result = ec2.DescribeInstances(req).DescribeInstancesResult.Reservation;
foreach (Amazon.EC2.Model.Reservation reservation in result)
{
foreach (Amazon.EC2.Model.RunningInstance runningInstance in reservation.RunningInstance)
{
Console.WriteLine(runningInstance.InstanceId);
Console.WriteLine(runningInstance.InstanceType);
}
}