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!