gpt4 book ai didi

java - 无法执行 HTTP 请求

转载 作者:行者123 更新时间:2023-12-05 05:31:45 24 4
gpt4 key购买 nike

我正在尝试使用 java sdk 获取附加到用户的 aws ec2 实例和 s3 存储桶的列表。我的主要程序类是

`

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 存储桶返回存储桶列表的响应,但 ec2 实例的响应抛出以下错误

无法执行 HTTP 请求:javax.xml.stream.XMLStreamException:ParseError at [row,col]:[29070,4] 消息:连接重置

我的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>

`请问我该怎么做才能返回正确的响应,即附加到我的 aws 帐户的 ec2 实例列表

最佳答案

我已经解决了。我使用 AmazonEC2AsyncClientBuilder 而不是 AmazonEC2ClientBuilder

关于java - 无法执行 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74296818/

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