AWS在Java中获取实例名称

时间:2017-01-22 07:25:20

标签: java amazon-web-services amazon-ec2 aws-sdk

我正在用Java开发应用程序,我能够列出实例:

Service.CreateCommand("delete")
       .Do(async (e) => {
           var MessageToDelete = await e.Channel.DownloadMessages(150);
           await e.Channel.DeleteMessages(MessageToDelete);
});

如何获取实例名称?

2 个答案:

答案 0 :(得分:6)

您可以访问与实例关联的标记:

for (Reservation reservation : result.getReservations()) {
    for (Instance instance : reservation.getInstances()) {
        System.out.println("Instance id:" + instance.getInstanceId());

        if (instance.getTags() != null) {
            for (Tag tag : instance.getTags()) {
                System.out.println(String.format(
                    "%s: %s", 
                    tag.getKey(), 
                    tag.getValue()
                ));
            }
        }
    }
}

答案 1 :(得分:0)

要获取实例名称,您需要使用以下代码:

        for (Reservation reservation : response.getReservations()) {
            for (Instance instance : reservation.getInstances()) {
                Tag tagName = instance.getTags().stream()
                        .filter(o -> o.getKey().equals("Name"))
                        .findFirst()
                        .orElse(new Tag("Name", "name not found"));

                System.out.println("Found instance with ID: " + instance.getInstanceId()
                        + ", NAME: " + tagName.getValue()
                        + ", TYPE: " + instance.getInstanceType()
                );
            }
        }

通过这种方式,您可以提取具有键:“名称”和值的标签。