gpt4 book ai didi

amazon-web-services - 我们如何检索 ec2 客户端标签

转载 作者:行者123 更新时间:2023-12-03 07:30:54 25 4
gpt4 key购买 nike

如何使用 Java aws v2 库检索 ec2 实例的标签。我已经被困在这个问题有一段时间了。我想要应用的过滤器是资源 id,但它给了我一个语法错误

DescribeTagsRequest req = DescribeTagsRequest.builder().filters(new Filter())

最佳答案

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.*;
import java.util.List;

public class DescribeInstanceTags {

public static void main(String[] args) {

String resourceId = args[0]; // Get this value from AWS Management Console
Region region = Region.US_EAST_1;
Ec2Client ec2 = Ec2Client.builder()
.region(region)
.build();

describeEC2Tags(ec2, resourceId);
ec2.close();
}

public static void describeEC2Tags(Ec2Client ec2, String resourceId ) {

try {

Filter filter = Filter.builder()
.name("resource-id")
.values(resourceId)
.build();

DescribeTagsResponse describeTagsResponse = ec2.describeTags(DescribeTagsRequest.builder().filters(filter).build());
List<TagDescription> tags = describeTagsResponse.tags();
for (TagDescription tag: tags) {
System.out.println("Tag key is: "+tag.key());
System.out.println("Tag value is: "+tag.value());
}

} catch ( Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}

关于amazon-web-services - 我们如何检索 ec2 客户端标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66036499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com