[英]AWS get instance name in Java
我正在用 Java 開發一個應用程序,我能夠列出實例:
for (Reservation reservation : result.getReservations()) {
for (Instance instance : reservation.getInstances()) {
System.out.println("Instance id:"
+ instance.getInstanceId());
}
}
如何獲取實例名稱?
您可以訪問與實例關聯的標簽:
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()
));
}
}
}
}
要獲取實例名稱,您需要使用以下內容:
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()
);
}
}
通過這種方式,您可以提取具有鍵:“名稱”和值的標簽,這正是您所需要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.