S3 cross region list buckets not working with the Java SDK

0

Hi all. I'm trying to do a list buckets operation from an EC2 instance with an IAM role attached which has the necessary permissions. The instance is in region us-east-1. When I try to do the list operation from the AWS CLI using the region override, I'm able to list the buckets.

aws s3 ls --region us-east-2

But when I try to do the same using the Java SDK, I get a ConnectionTimedOut exception. The client is built as follows which is used to do the list operation.

AmazonS3ClientBuilder s3ClientBuilder = AmazonS3ClientBuilder.standard();
return s3ClientBuilder
                .withRegion("us-east-2")
                .withForceGlobalBucketAccessEnabled(true)
                .build();

Code to print the list of buckets

List<Bucket> buckets = s3Client.listBuckets();
buckets.forEach(System.out::println);

Can anyone please help in what should I check for?

  • Hi, you should paste full code (i.e. including your list request) to obtain efficient support. BTW, it seems that you're still using SDK v1. Why don't you switch to v2?

  • @Didier_Durand Added the code for listing the buckets. As for the v1 SDK, I was just trying out with this code and got an issue.

1回答
0

Hi, such a ConnectionTimeout usually comes from the fact that the EC2 instance cannot access the S3 service endpoint. The issue then lies usually in the security group of the VPC where the EC2 instance resides: it must allow access to Internet for https (tcp port 443).

If your EC2 needs to be in a fully private VPC, you'll have to define a service endpoint for S3 in the VPC.: see https://docs.aws.amazon.com/AmazonS3/latest/userguide/privatelink-interface-endpoints.html

Note: I'd suggest to get this code working on your laptop first before moving it to EC2. It reduces the number of potential config hiccups. When ok on your laptop, you can move it to EC2 and focus on the config of your resources.

Best,

Didier

profile pictureAWS
エキスパート
回答済み 9ヶ月前
  • Thanks for your response, I'll check the security group of the VPC. But do you have any idea why it works with the AWS CLI and not with the Java SDK? Does the AWS CLI performs some additional steps before making the list operation? The Java SDK works without the region parameter and I'm facing this issue only when I provide the region as us-east-2.

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ