No method for setObjectAcl in AWS Java SDK v2.x

103 views Asked by At

I am upgrading a legacy java application that used AWS SDK v1.5 for S3 operations, and one of the functions it had to do was to change an objects ACL, from private to public-read, based on certain conditions. The v1.5 AmazonS3 had a method for this, simple as:

s3client.setObjectAcl(bucketName, s3Key, acl);

But it appears that feature or capability doesn't exist in v2.21.2. I've looked through all the posted examples and documentation, and found a rather complex example of how to change the ACL for a bucket, but I need to be able to set a single object acl to public-read or back to private, not the whole bucket.

How do I do this in AWS SDK v2.21.x?

3

There are 3 answers

1
Quassnoi On BEST ANSWER

In v2, they renamed some of the methods to match API action names:

To provide SDK support for the many services that AWS owns, the AWS SDKs make extensive use of code generation. In 1.11.x, all service clients are generated, except for the S3 client. This frequently results in a disconnect between how non-Java AWS SDKs and IAM policies refer to an S3 operation (e.g., DeleteBucketReplicationConfiguration), and how the Java AWS SDK would refer to that same operation (DeleteBucketReplication). This made creating IAM policies and switching to other SDKs difficult, because the equivalent string was not always well documented.

In 2.x, S3 is generated like every other service, ensuring the operation names, inputs, and outputs will always match those of other SDKs and IAM.

You're looking for putObjectAcl, which is a wrapper for PutObjectAcl API action.

0
user2989397 On

Quasnoi pointed me in the right direction. My method ended up looking something like this:

    public void setObjectAcl(String bucket, String key, ObjectCannedACL acl) {
    
    try {
        PutObjectAclRequest putObjectAclRequest = PutObjectAclRequest.builder()
                .bucket(bucket)
                .key(key)
                .acl(acl)
                .build();
        
        s3client.putObjectAcl(putObjectAclRequest);
        
    } catch (Exception e) {
        logger.error("Error setting new ACL on " + key + ":" + e.getMessage());
    }
    
}
0
Mark B On

I went to the Java SDK S3 Client documentation and just did a full page search for "ACL". I see there are two putObjectAcl methods, which are exactly what you are looking for.