How do you determine your permissions in AWS S3 through the Java SDK?

Viewed 4485

I know you can try to read the ACLs or Bucket Policies through the Java SDK, but is there any easy way to just check if you have read and/or write permissions to a bucket and/or its contents? I don't see any "haveReadPermissions()" method or anything in the AmazonS3 class, but maybe I'm missing something? I find it hard to believe there's no easy way to check permissions.

5 Answers

Here how you can do this:

// get list of bucket permission
List<String> bucketPermissions = s3Client
    .getBucketAcl(bucketName)
    .getGrantsAsList().stream().distinct()
    .map(t -> t.getPermission().toString())
    .collect(Collectors.toList());

// check read/write or full control permission
if (
    !((bucketPermissions.contains("READ")
    && bucketPermissions.contains("WRITE"))
    || (bucketPermissions.contains("FULL_CONTROL")))) {
throw new InsufficentBucketPermissionException();
}

Please feel free to improve this solution.

Related