Example usage for com.amazonaws.util StringUtils isNullOrEmpty

List of usage examples for com.amazonaws.util StringUtils isNullOrEmpty

Introduction

In this page you can find the example usage for com.amazonaws.util StringUtils isNullOrEmpty.

Prototype

public static boolean isNullOrEmpty(String value) 

Source Link

Usage

From source file:org.reswitchboard.utils.s3.access.App.java

License:Open Source License

public static void main(String[] args) {
    try {/*w w w. j  a  va  2 s .  co m*/
        if (args.length == 0 || StringUtils.isNullOrEmpty(args[0]))
            throw new IllegalArgumentException("Bucket name can not be empty");

        String bucketName = args[0];
        String prefix = null;
        if (args.length > 1)
            prefix = args[1];

        AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName);

        if (!StringUtils.isNullOrEmpty(prefix))
            listObjectsRequest.setPrefix(prefix);

        ObjectListing objectListing;

        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                String key = objectSummary.getKey();
                System.out.println(" - " + key);

                for (int nAttempt = 1;; ++nAttempt) {
                    try {

                        AccessControlList acl = s3client.getObjectAcl(bucketName, key);
                        List<Grant> grants = acl.getGrantsAsList();
                        for (Grant grant : grants) {
                            //   System.out.println( "      Grant: " + grant.toString());

                            if (grant.getGrantee().equals(GroupGrantee.AllUsers)) {
                                System.out.println("      Revoking public access");

                                acl.revokeAllPermissions(GroupGrantee.AllUsers);
                                s3client.setObjectAcl(bucketName, key, acl);

                                break;
                            }
                        }

                        break;
                    } catch (Exception e) {
                        System.out.println("Error: " + e.toString());

                        if (nAttempt >= 10) {
                            throw new Exception("Maximum number of invalid attempts has been reeched");
                        }

                        // double back-off delay
                        Thread.sleep((long) (Math.pow(2, nAttempt) * 50));
                    }
                }

            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (Exception e) {
        e.printStackTrace();
    }
}