Example usage for com.amazonaws.services.s3 AmazonS3 setObjectAcl

List of usage examples for com.amazonaws.services.s3 AmazonS3 setObjectAcl

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3 setObjectAcl.

Prototype

public void setObjectAcl(String bucketName, String key, CannedAccessControlList acl)
        throws SdkClientException, AmazonServiceException;

Source Link

Document

Sets the CannedAccessControlList for the specified object in Amazon S3 using one of the pre-configured CannedAccessControlLists.

Usage

From source file:squash.deployment.lambdas.utils.TransferUtils.java

License:Apache License

/**
 * Sets public read permissions on content within an S3 bucket.
 * //from w  w w  .j  ava2  s.  c  o m
 * <p>Web content served from an S3 bucket must have public read permissions.
 * 
 *    @param bucketName the bucket to apply the permissions to.
 *    @param prefix prefix within the bucket, beneath which to apply the permissions.
 *    @param logger a CloudwatchLogs logger.
 */
public static void setPublicReadPermissionsOnBucket(String bucketName, Optional<String> prefix,
        LambdaLogger logger) {
    // Ensure newly uploaded content has public read permission
    ListObjectsRequest listObjectsRequest;
    if (prefix.isPresent()) {
        logger.log("Setting public read permission on bucket: " + bucketName + " and prefix: " + prefix.get());
        listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(prefix.get());
    } else {
        logger.log("Setting public read permission on bucket: " + bucketName);
        listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName);
    }

    ObjectListing objectListing;
    AmazonS3 client = TransferManagerBuilder.defaultTransferManager().getAmazonS3Client();
    do {
        objectListing = client.listObjects(listObjectsRequest);
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            logger.log("Setting permissions for S3 object: " + objectSummary.getKey());
            client.setObjectAcl(bucketName, objectSummary.getKey(), CannedAccessControlList.PublicRead);
        }
        listObjectsRequest.setMarker(objectListing.getNextMarker());
    } while (objectListing.isTruncated());
    logger.log("Finished setting public read permissions");
}