gpt4 book ai didi

java - 在 EC2 实例上运行时,Amazon S3 不返回响应或抛出错误

转载 作者:行者123 更新时间:2023-12-01 18:13:31 28 4
gpt4 key购买 nike

我正在尝试使用访问 key 和 key 凭据连接到 S3 存储桶。这在我的本地机器上可以正常工作。但是,当我尝试在 EC2 实例上运行它时,执行似乎停止在 result = s3Client.listObjectsV2(request); 行。没有异常(exception)。根本没有任何反应。我真的很感激任何帮助。

Java代码

    AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accesskey, secretkey)))
.withRegion(region).build();

ListObjectsV2Result result = null;
List<S3ObjectSummary> objects = null;
String continuationToken = null;

System.out.println("Starting loop to request information");

int count = 1;
do {
ListObjectsV2Request request = new ListObjectsV2Request();
request.setBucketName(bucket);
request.setContinuationToken(continuationToken);

System.out.println("Placing request information #" + count);
result = s3Client.listObjectsV2(request);
System.out.println("Got response for request #" + count++);

continuationToken = result.getNextContinuationToken();
objects = result.getObjectSummaries();

for (S3ObjectSummary os : objects) {
System.out.println(os.getKey());
}
} while (continuationToken != null);

pom.xml

    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.466</version>
</dependency>

S3 存储桶策略

{
"Version": "2012-10-17",
"Id": "Policy1563965234895",
"Statement": [
{
"Sid": "Stmt1563965231235",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::xxxxxxxxxxxx:user/xyz_dev",
]
},
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::xxxx-yyy-bucket",
"arn:aws:s3:::xxxx-yyy-bucket/*"
]
}
]
}

最佳答案

感谢您的回复。我的代码存在多个问题(这不是 Amazon S3 的问题)

  1. 这是臭名昭著的错误 java.lang.NoSuchFieldError: SIGNING_REGION,但仅发生在 EC2 上。它没有被代码周围的 try-catch block 捕获,而是在 HTTP 响应中。
  2. 我的项目基于 Spring Boot,它错误地导入了不同版本的 aws-sdk 模块
  3. 我有另一个 POM 条目 hadoop-aws,它有自己的 aws-sdk 版本

修复:

  1. 添加了单独的 aws-sdk 模块条目,而不是完整的 aws-java-sdk 亚马逊网站 aws-java-sdk-cognitoidp

    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-core -->
    <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-core</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-cognitoidentity -->
    <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-cognitoidentity</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-cognitoidp -->
    <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-cognitoidp</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-kms -->
    <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-kms</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
    <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    </dependency>
  2. 向 hadoop-aws 添加了排除项

    <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-aws</artifactId>
    <version>3.1.1</version>
    <exclusions>
    <exclusion>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-bundle</artifactId>
    </exclusion>
    </exclusions>
    </dependency>

关于java - 在 EC2 实例上运行时,Amazon S3 不返回响应或抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60418326/

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