简体   繁体   English

使用AWS Java SDK获取EC2实例XML描述?

[英]Get EC2 Instance XML Description using AWS Java SDK?

We have a scenario in which we need to retrieve the description info for EC2 instances running on AWS. 我们有一个场景,我们需要检索在AWS上运行的EC2实例的描述信息。 To accomplish this, we are using the AWS Java SDK. 为此,我们使用AWS Java SDK。 In 90% of our use case, the com.amazonaws.services.ec2.model.Instance class is exactly what we need. 在我们90%的用例中, com.amazonaws.services.ec2.model.Instance类正是我们所需要的。 However, there is also a small use-case where it would be beneficial to get the raw XML describing the instance. 但是,还有一个小用例,获取描述实例的原始XML将是有益的。 That is, the XML data before it is converted into the Instance object. 也就是说,之前的XML数据被转换为Instance对象。 Is there any way to obtain both the Instance object and the XML string using the AWS Java SDK? 有没有办法使用AWS Java SDK同时获取Instance对象 XML字符串? Is there a way to manually convert from one to the other? 有没有办法手动从一个转换到另一个? Or, would we be forced to make a separate call using HttpClient or something similar to get the XML data? 或者,我们是否会被迫使用HttpClient或类似的东西进行单独的调用来获取XML数据?

Make an EC2Client by adding request handler and override the beforeUnmarshalling() method like below 通过添加请求处理程序创建EC2Client并覆盖beforeUnmarshalling()方法,如下所示

AmazonEC2ClientBuilder.standard().withRegion("us-east-1")
    .withRequestHandlers(
            new RequestHandler2() {
                  @Override
                    public HttpResponse beforeUnmarshalling(Request<?> request, HttpResponse httpResponse) {
                        // httpResponse.getContent() is the raw xml response from AWS
                        // you either save it to a file or to a XML document
                        return new HTTPResponse(...);
                        // if you consumed httpResponse.getContent(), you need to provide new HTTPResponse
                    }
                }
        ).build():

You could use JAXB .marshal like following. 您可以使用JAXB .marshal,如下所示。 JAXB (Java Architecture for XML Binding) could convert Java object to / from XML file. JAXB (用于XML绑定的Java体系结构)可以将Java对象转换为XML文件或从XML文件转换。

StringWriter sw = new StringWriter();
JAXB.marshal(instance, sw);
String xmlString = sw.toString();

If you have xml (eg from using AWS rest API directly), then you can use com.amazonaws.services.ec2.model.transform.* classes to convert xml to java objects. 如果你有xml(例如直接使用AWS rest API),那么你可以使用com.amazonaws.services.ec2.model.transform.*类将xml转换为java对象。 Unfortunately, it only provides classes required for SDK itself. 不幸的是,它只提供SDK本身所需的类。 So you, for example, can convert raw XML to an Instance using InstanceStaxUnmarshaller, but can't convert Instance to XML unless you write such converter. 例如,您可以使用InstanceStaxUnmarshaller将原始XML转换为实例,但除非您编写此类转换器,否则无法将实例转换为XML。

Here is an example how to parse an Instance XML: 以下是如何解析实例XML的示例:

XMLEventReader eventReader = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(instanceXml));
StaxUnmarshallerContext suc = new StaxUnmarshallerContext(eventReader, new TreeMap<>());
InstanceStaxUnmarshaller isu = new InstanceStaxUnmarshaller();
Instance i = isu.unmarshall(suc);
System.out.println(i.toString());

You probably can try to intercept raw AWS response, so that you can keep raw XML while still using SDK most of the time. 您可能可以尝试拦截原始AWS响应,以便您可以在大多数时间仍使用SDK的同时保留原始XML。 But I wouldn't call that easy as it will require quite a bit of coding. 但我不会这么简单,因为它需要相当多的编码。

You can use AWS rest API to replace Java SDK. 您可以使用AWS rest API替换Java SDK。 A bonus will be slight performance gain because you'll not send statistic data to Amazon as the SDK does. 奖励将是轻微的性能提升,因为您不会像SDK那样向亚马逊发送统计数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM