我们如何检索 ec2 客户端标签

How do we retrieve ec2 client tags

如何使用 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.DescribeTagsRequest;
import software.amazon.awssdk.services.ec2.model.DescribeTagsResponse;
import software.amazon.awssdk.services.ec2.model.Filter;

public class Ec2Tags {

    public static void main(String[] args) {
        Ec2Client ec2 = Ec2Client.builder().region(Region.EU_WEST_1).build(); // Change ec2 client logic as per your setup
        Filter resource_id_filter = Filter.builder().name("resource-id").values("i-0fd113").build();
        Filter resource_type_filter = Filter.builder().name("resource-type").values("instance").build();
        DescribeTagsResponse describeTagsResponse = ec2.describeTags(DescribeTagsRequest.builder().filters(resource_type_filter, resource_id_filter).build());
        describeTagsResponse.tags().forEach(tag -> {
                System.out.println("Resource ID = " + tag.resourceId());
                System.out.println(" Tag Key =" + tag.key());
                System.out.println(" Tag Value =" + tag.value());
        });


    }
}

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);
        }
    }
}