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

时间:2022-03-23 17:14:47

We have a scenario in which we need to retrieve the description info for EC2 instances running on AWS. To accomplish this, we are using the AWS Java SDK. In 90% of our use case, the com.amazonaws.services.ec2.model.Instance class is exactly what we need. However, there is also a small use-case where it would be beneficial to get the raw XML describing the instance. That is, the XML data before it is converted into the Instance object. Is there any way to obtain both the Instance object and the XML string using the AWS Java SDK? 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?

我们有一个场景,我们需要检索在AWS上运行的EC2实例的描述信息。为此,我们使用AWS Java SDK。在我们90%的用例中,com.amazonaws.services.ec2.model.Instance类正是我们所需要的。但是,还有一个小用例,获取描述实例的原始XML是有益的。也就是说,它之前的XML数据被转换为Instance对象。有没有办法使用AWS Java SDK同时获取Instance对象和XML字符串?有没有办法手动从一个转换到另一个?或者,我们是否会*使用HttpClient或类似的东西进行单独调用来获取XML数据?

4 个解决方案

#1


1  

If you have xml (e.g. from using AWS rest API directly), then you can use com.amazonaws.services.ec2.model.transform.* classes to convert xml to java objects. Unfortunately, it only provides classes required for SDK itself. 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.

如果你有xml(例如直接使用AWS rest API),那么你可以使用com.amazonaws.services.ec2.model.transform。*类将xml转换为java对象。不幸的是,它只提供SDK本身所需的类。例如,您可以使用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. But I wouldn't call that easy as it will require quite a bit of coding.

您可能可以尝试拦截原始AWS响应,以便您可以在大多数时间仍使用SDK的同时保留原始XML。但我不会那么容易,因为它需要相当多的编码。

#2


2  

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():

#3


1  

You could use JAXB.marshal like following. JAXB (Java Architecture for XML Binding) could convert Java object to / from XML file.

您可以使用JAXB.marshal,如下所示。 JAXB(用于XML绑定的Java体系结构)可以将Java对象转换为XML文件或从XML文件转换。

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

#4


0  

You can use AWS rest API to replace Java SDK. A bonus will be slight performance gain because you'll not send statistic data to Amazon as the SDK does.

您可以使用AWS rest API替换Java SDK。奖励将是轻微的性能提升,因为您不会像SDK那样向Amazon发送统计数据。

#1


1  

If you have xml (e.g. from using AWS rest API directly), then you can use com.amazonaws.services.ec2.model.transform.* classes to convert xml to java objects. Unfortunately, it only provides classes required for SDK itself. 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.

如果你有xml(例如直接使用AWS rest API),那么你可以使用com.amazonaws.services.ec2.model.transform。*类将xml转换为java对象。不幸的是,它只提供SDK本身所需的类。例如,您可以使用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. But I wouldn't call that easy as it will require quite a bit of coding.

您可能可以尝试拦截原始AWS响应,以便您可以在大多数时间仍使用SDK的同时保留原始XML。但我不会那么容易,因为它需要相当多的编码。

#2


2  

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():

#3


1  

You could use JAXB.marshal like following. JAXB (Java Architecture for XML Binding) could convert Java object to / from XML file.

您可以使用JAXB.marshal,如下所示。 JAXB(用于XML绑定的Java体系结构)可以将Java对象转换为XML文件或从XML文件转换。

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

#4


0  

You can use AWS rest API to replace Java SDK. A bonus will be slight performance gain because you'll not send statistic data to Amazon as the SDK does.

您可以使用AWS rest API替换Java SDK。奖励将是轻微的性能提升,因为您不会像SDK那样向Amazon发送统计数据。