Search code examples
javaamazon-web-servicesamazon-s3amazon-ec2aws-java-sdk

Amazon S3 does not return a response or throw error when running on EC2 instance


I am attempting to connect to an S3 bucket using the access key and secret key credentials. This works correctly on my local machine. However, when I try to run it on an EC2 instance the execution seems to stop at the line result = s3Client.listObjectsV2(request);. There are no exceptions. There is simply no response. I would really appreciate any help.

Java code

    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 Bucket policy

{
    "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/*"
            ]
        }
    ]
}

Solution

  • Thank for your responses. I had multiple issues with the code (it was not an issue with the Amazon S3)

    1. It was the infamous error java.lang.NoSuchFieldError: SIGNING_REGION but only occurring on EC2. It was not caught in the try-catch block surrounding the code but was in the HTTP response.
    2. My project is on Spring Boot it had incorrectly imported different versions of aws-sdk modules
    3. I had another POM entry hadoop-aws that had its own version of aws-sdk

    Fix:

    1. Added individual aws-sdk module entries instead of the full aws-java-sdk com.amazonaws 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. Added exclusions to 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>