Search in sources :

Example 1 with BucketPolicy

use of com.amazonaws.services.s3.model.BucketPolicy in project aws-doc-sdk-examples by awsdocs.

the class GetBucketPolicy method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GetBucketPolicy <bucket>\n\n" + "Where:\n" + "    bucket - the bucket to get the policy from.\n\n" + "Example:\n" + "    GetBucketPolicy testbucket\n\n";
    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucket_name = args[0];
    String policy_text = null;
    System.out.format("Getting policy for bucket: \"%s\"\n\n", bucket_name);
    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {
        BucketPolicy bucket_policy = s3.getBucketPolicy(bucket_name);
        policy_text = bucket_policy.getPolicyText();
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    if (policy_text == null) {
        System.out.println("The specified bucket has no bucket policy.");
    } else {
        System.out.println("Returned policy:");
        System.out.println("----");
        System.out.println(policy_text);
        System.out.println("----\n");
    }
    System.out.println("Done!");
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) BucketPolicy(com.amazonaws.services.s3.model.BucketPolicy) AmazonServiceException(com.amazonaws.AmazonServiceException)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 BucketPolicy (com.amazonaws.services.s3.model.BucketPolicy)1