I am trying to read objects from a specific S3 bucket using Java sdk, but I am getting Access Denied error when trying to perform read objects.
I've generated temporary creds(access key and access id) using AWS cli, and the creds and config file got generated in .aws folder, and I've mentioned the same access key and access id in my system environment variables. The code is able to connect to S3 endpoint and check if the bucket is present, but not able to perform any read operations.
The required s3read policies are attached to the IAM role, via which we generate credentials from CLI and use the same in java code.
The bucket policy also looks fine as the block public access is off.
I am able to list the bucket's objects from AWS CLI, but failing to do so from Java SDK.Please help on this.
Please find the below error from Java SDK.
Exception occured while reading object from s3 bucketcom.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied;
To generate security credentials for specific profile I ran the samlapi command that was written for our application post which I configured the credentials generated in env variables which includes access id, secret access key and security token
The IAM role has AmazonS3ReadAccess policy attached and with respect to bucket policy no action is denied and public access is not blocked.
To list the bucket objects used
aws s3 ls s3:bucket-name --profile profile-name
Used the following Java code to connect to s3 client from java sdk and to list objects from s3 bucket
s3client = AmazonS3ClientBuilder.standard().withCredentials(new EnvironmentVariableCredentialsProvider()).build();
if(s3client.doesBucketExistV2(bucketName)) {
ListObjectsV2Result objectList = s3client.listObjectsV2(bucketName);
List<S3ObjectSummary> s3ObjSummaryList = objectList.getObjectSummaries();
}