//snippet-sourcedescription:[GetBucketPolicy.java demonstrates how to get the bucket policy for an existing S3 bucket]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[s3]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[2020-02-06]
//snippet-sourceauthor:[scmacdon]
/*
Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
package com.example.s3;
// snippet-start:[s3.java2.get_bucket_policy.complete]
// snippet-start:[s3.java2.get_bucket_policy.import]
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetBucketPolicyRequest;
import software.amazon.awssdk.services.s3.model.GetBucketPolicyResponse;
// snippet-end:[s3.java2.get_bucket_policy.import]

/**
 * Gets the bucket policy from an existing S3 bucket.
 *
 * This code expects that you have AWS credentials set up, as described here:
 * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html
 */
// snippet-start:[s3.java2.get_bucket_policy.main]
public class GetBucketPolicy {

    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 bucket1\n\n";

        if (args.length < 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String bucketName = args[0];
        String policyText = null;

        System.out.format("Getting policy for bucket: \"%s\"\n\n", bucketName);

        Region region = Region.US_WEST_2;
        S3Client s3 = S3Client.builder().region(region).build();

        GetBucketPolicyRequest policyReq = GetBucketPolicyRequest.builder()
                .bucket(bucketName)
                .build();

        try {
            GetBucketPolicyResponse policyRes = s3.getBucketPolicy(policyReq);
            policyText = policyRes.policy();
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }

        if (policyText == null) {
            System.out.println("The specified bucket has no bucket policy.");
        } else {
            System.out.println("Returned policy:");
            System.out.println("----");
            System.out.println(policyText);
            System.out.println("----\n");
        }

        System.out.println("Done!");
    }
}
// snippet-end:[s3.java2.get_bucket_policy.main]
// snippet-end:[s3.java2.get_bucket_policy.complete]

@@ -93,32 +93,39 @@
 for an existing 
+Amazon 
 S3 bucket%5D%0D%0A//sn
@@ -210,17 +210,24 @@
 ervice:%5B
-s
+Amazon S
 3%5D%0D%0A//sn
@@ -286,17 +286,15 @@
 e:%5B2
-020-02-06
+/6/2020
 %5D%0D%0A/
@@ -324,16 +324,20 @@
 scmacdon
+-aws
 %5D%0D%0A/*%0D%0AC
@@ -349,18 +349,8 @@
 ght 
-2010-2020 
 Amaz
@@ -872,62 +872,8 @@
 3;%0D%0A
-// snippet-start:%5Bs3.java2.get_bucket_policy.complete%5D
 %0D%0A//
@@ -1525,58 +1525,8 @@
 */%0D%0A
-// snippet-start:%5Bs3.java2.get_bucket_policy.main%5D
 %0D%0Apu
@@ -1919,24 +1919,26 @@
 %0D%0A%0D%0A        
+  
 if (args.len
@@ -1945,24 +1945,25 @@
 gth %3C 1) %7B%0D%0A
+ 
             
@@ -2019,32 +2019,33 @@
 it(1);%0D%0A        
+ 
 %7D%0D%0A%0D%0A        Str
@@ -2075,45 +2075,8 @@
 %5D;%0D%0A
-        String policyText = null;%0D%0A%0D%0A
     
@@ -2241,30 +2241,427 @@
 er()
-.region(region).build(
+%0D%0A                .region(region)%0D%0A                .build();%0D%0A%0D%0A        String polText = GetPolicy(s3, bucketName );%0D%0A        System.out.println(%22Policy Text: %22+polText);%0D%0A    %7D%0D%0A%0D%0A    // snippet-start:%5Bs3.java2.get_bucket_policy.main%5D%0D%0A    public static String GetPolicy(S3Client s3, String bucketName) %7B%0D%0A%0D%0A        String policyText = %22%22;%0D%0A        System.out.format(%22Getting policy for bucket: %5C%22%25s%5C%22%5Cn%5Cn%22, bucketName
 );%0D%0A
@@ -2942,24 +2942,56 @@
 .policy();%0D%0A
+            return policyText;%0D%0A
         %7D ca
@@ -3122,26 +3122,24 @@
      %7D%0D%0A
-%0D%0A
         
 if (poli
@@ -3134,362 +3134,17 @@
     
-if (policyText == null) %7B%0D%0A            System.out.println(%22The specified bucket has no bucket policy.%22);%0D%0A        %7D else %7B%0D%0A            System.out.println(%22Returned policy:%22);%0D%0A            System.out.println(%22----%22);%0D%0A            System.out.println(policyText);%0D%0A            System.out.println(%22----%5Cn%22);%0D%0A        %7D%0D%0A%0D%0A        System.out.println(%22Done!%22)
+return %22%22
 ;%0D%0A 
@@ -3149,19 +3149,16 @@
 %0A    %7D%0D%0A
-%7D%0D%0A
 // snipp
@@ -3203,58 +3203,7 @@
 n%5D%0D%0A
-// snippet-end:%5Bs3.java2.get_bucket_policy.complete%5D
+%7D
 %0D%0A

Update GetBucketPolicy.java