0
votes

I am trying to fetch the list of buckets in my S3Client but I am getting exception as com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID:xxxxxxxxx; S3 Extended Request ID: xxxxxx=; xxxxx: xxxxxxx=

[junit] at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1819) [junit] at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleServiceErrorResponse(AmazonHttpClient.java:1403) [junit] at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1372) [junit] at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1145) [junit] at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:802) [junit] at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:770) [junit] at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:744) [junit] at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:704) [junit] at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:686) [junit] at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:550) [junit] at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:530) [junit] at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:5248) [junit] at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:5195) [junit] at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:5189) [junit] at com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:1018) [junit] at com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:1024) [junit] at src.projectname.tst.S3Accessor.test2(S3Accessor.java:71)

My code :


    public void readBucket() throws IOException {
        String REGION = "us-east-2";
        String bucketName = "bucketName";
        String key = "objectName";
        try {
            AWSCredentials credentials = new BasicAWSCredentials("xxxxxxxx","xxxxxx");

            AmazonS3 s3Client = AmazonS3ClientBuilder
                    .standard()
                    .withCredentials(new AWSStaticCredentialsProvider(credentials))
                    .withRegion(Regions.US_EAST_2)
                    .build();
            if (s3Client.doesBucketExist("bucket name")) {
                System.out.println("Bucket %s already exists.\n");
            }
            List<Bucket> buckets = s3Client.listBuckets();
            System.out.println("Your Amazon S3 buckets are:");
            for (Bucket b : buckets) {
                System.out.println("* " + b.getName());
            }
          }
   }

the policy I attached with IAM User is-

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "arn:aws:s3:::bucket name",
                "arn:aws:s3:::bucketname/*"
            ]
        }
    ]
}

I am actually able to check if my bucket exists correctly i.e my s3Client is updated correctly.Can anyone help me with this. Thanks in advance!

1

1 Answers

0
votes

Its a credential permission issue. Try granting full S3 permission to the IAM role/user that corresponds to the creds you are using. Once your permissions are properly set, there are no issues with invoking Amazon S3 operations using the Amazon S3 Java API.

Also, consider moving away from the older V1 API to the new V2 API:

The AWS SDK for Java 2.x is a major rewrite of the version 1.x code base. It’s built on top of Java 8+ and adds several frequently requested features. These include support for non-blocking I/O and the ability to plug in a different HTTP implementation at run time.

You can find many S3 V2 code examples here:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/s3/src/main/java/com/example/s3

How are you setting your permissions - via a custom policy or using this one?

enter image description here