Path: blob/master/code_examples/java_examples/S3Examples/ModifyACLExistingObject.java
4084 views
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)23import java.io.IOException;45import com.amazonaws.AmazonServiceException;6import com.amazonaws.SdkClientException;7import com.amazonaws.auth.profile.ProfileCredentialsProvider;8import com.amazonaws.services.s3.AmazonS3;9import com.amazonaws.services.s3.AmazonS3ClientBuilder;10import com.amazonaws.services.s3.model.AccessControlList;11import com.amazonaws.services.s3.model.CanonicalGrantee;12import com.amazonaws.services.s3.model.EmailAddressGrantee;13import com.amazonaws.services.s3.model.Permission;1415public class ModifyACLExistingObject {1617public static void main(String[] args) throws IOException {18String clientRegion = "*** Client region ***";19String bucketName = "*** Bucket name ***";20String keyName = "*** Key name ***";21String emailGrantee = "*** [email protected] ***";2223try {24AmazonS3 s3Client = AmazonS3ClientBuilder.standard()25.withCredentials(new ProfileCredentialsProvider())26.withRegion(clientRegion)27.build();2829// Get the existing object ACL that we want to modify.30AccessControlList acl = s3Client.getObjectAcl(bucketName, keyName);3132// Clear the existing list of grants.33acl.getGrantsAsList().clear();3435// Grant a sample set of permissions, using the existing ACL owner for Full Control permissions.36acl.grantPermission(new CanonicalGrantee(acl.getOwner().getId()), Permission.FullControl);37acl.grantPermission(new EmailAddressGrantee(emailGrantee), Permission.WriteAcp);3839// Save the modified ACL back to the object.40s3Client.setObjectAcl(bucketName, keyName, acl);41}42catch(AmazonServiceException e) {43// The call was transmitted successfully, but Amazon S3 couldn't process44// it, so it returned an error response.45e.printStackTrace();46}47catch(SdkClientException e) {48// Amazon S3 couldn't be contacted for a response, or the client49// couldn't parse the response from Amazon S3.50e.printStackTrace();51}52}53}5455