简体   繁体   中英

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

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>

You mention that your project is a Spring BOOT project.

We are working on a document that will show you how to write a Spring BOOT application that invokes AWS Services (in the document, DynamoDB is used as an example) and deploy it to AWS Elastic Beanstalk.

When you do so, there are a few things you need to do to make a Spring BOOT app work, such as:

  1. Set the port that Spring Boot listens on by adding a new environment variable named SERVER_PORT , with the value 5000.

  2. Add a new variable named AWS_ACCESS_KEY_ID and specify your access key value.

  3. Add a new variable named AWS_SECRET_ACCESS_KEY and specify your secret key value.

To create an AWS Service client, use a EnvironmentVariableCredentialsProvider - like this to use the environment variables.

Region region = Region.US_EAST_1; DynamoDbClient ddb =

 DynamoDbClient.builder()
             .region(region)
             .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
             .build();

When the document is done, I will post it here.

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM