S3 cross region list buckets not working with the Java SDK


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

Code to print the list of buckets

List<Bucket> buckets = s3Client.listBuckets();

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 Answer

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.



profile pictureAWS
answered 8 months ago
  • 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.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions