我正在尝试使用java sdk来获取aws ec2示例和附加到用户的s3 buckets的列表。我的主程序类是
`
package com.aws.demo;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.Image;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
@SpringBootApplication()
public class AwsApplication {
public static void main(String[] args) {
SpringApplication.run(AwsApplication.class, args);
final String aws_access_key_id = ""; // deleted for security reasons
final String aws_secret_access_key = ""; // deleted for security reasons
BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_access_key_id, aws_secret_access_key);
try { AmazonS3 s3 = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion("us-east-1")
.build();
List<Bucket> buckets = s3.listBuckets();
buckets.stream().forEach(bucket ->{
System.out.println("Bucket Name : "+ bucket.getName()+", Bucket Owner : "+bucket.getOwner().getDisplayName()
+", Bucket Creation Date: "+ bucket.getCreationDate());
});
AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion("us-east-1")
.build();
List<Image> listEc2s = amazonEC2.describeImages().getImages();
listEc2s.stream().forEach(image->{
System.out.println("Image id : "+image.getImageId()+"Image owner : "+image.getImageOwnerAlias());
});
System.out.println("Image list"+amazonEC2.describeImages().getImages());
}
catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace().toString());
}
}
}
's3 bucket返回bucket列表的响应,但ec2示例的bucket返回以下错误Unable to execute HTTP request: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[29070,4] Message: Connection reset
我的pom.xml文件是
`
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.aws-services</groupId>
<artifactId>aws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aws</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.379</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-ec2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
'请问我能做些什么来返回正确的响应,ec2示例的列表附加到我的aws帐户
1条答案
按热度按时间uqcuzwp81#
我已经修复了它。我使用的是AmazonEC2AsyncClientBuilder而不是AmazonEC2ClientBuilder