diff --git a/.doc_gen/metadata/s3-control_metadata.yaml b/.doc_gen/metadata/s3-control_metadata.yaml index 589285de221..5b34a0a4eac 100644 --- a/.doc_gen/metadata/s3-control_metadata.yaml +++ b/.doc_gen/metadata/s3-control_metadata.yaml @@ -33,7 +33,7 @@ s3-control_CreateJob: - description: Create a legal hold off job. snippet_tags: - s3control.java2.create_job.compliance.main - - description: Create a new governance retemtion job. + - description: Create a new governance retention job. snippet_tags: - s3.java2.create_governance_retemtion.main services: diff --git a/javav2/example_code/s3/pom.xml b/javav2/example_code/s3/pom.xml index 3c25b740083..957e73c9091 100644 --- a/javav2/example_code/s3/pom.xml +++ b/javav2/example_code/s3/pom.xml @@ -48,7 +48,7 @@ - + @@ -185,4 +185,4 @@ 2.10.1 - \ No newline at end of file + \ No newline at end of file diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java b/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java index 4717df9de74..b5c4ef0a070 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java @@ -6,10 +6,7 @@ // snippet-start:[s3.java2.list.buckets.main] import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; -import software.amazon.awssdk.services.s3.model.Bucket; -import software.amazon.awssdk.services.s3.model.ListBucketsResponse; -import java.util.List; - +import software.amazon.awssdk.services.s3.paginators.ListBucketsIterable; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. @@ -35,11 +32,8 @@ public static void main(String[] args) { * @param s3 The {@link S3Client} instance to use for interacting with the Amazon S3 service. */ public static void listAllBuckets(S3Client s3) { - ListBucketsResponse response = s3.listBuckets(); - List bucketList = response.buckets(); - for (Bucket bucket: bucketList) { - System.out.println("Bucket name "+bucket.name()); - } + ListBucketsIterable response = s3.listBucketsPaginator(); + response.buckets().forEach(bucket -> + System.out.println("Bucket Name: " + bucket.name())); } -} -// snippet-end:[s3.java2.list.buckets.main] \ No newline at end of file +}// snippet-end:[s3.java2.list.buckets.main] \ No newline at end of file diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateObjectLockRole.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateObjectLockRole.java new file mode 100644 index 00000000000..6cc3c9ea2df --- /dev/null +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateObjectLockRole.java @@ -0,0 +1,107 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + + +package com.example.s3.batch; + +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.iam.IamClient; +import software.amazon.awssdk.services.iam.model.CreateRoleRequest; +import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest; + +public class CreateObjectLockRole { + public static void main(String[] args) { + final String usage = """ + + Usage: + + Where: + roleName - the IAM role name. + """; + + if (args.length != 1) { + System.out.println(usage); + System.exit(1); + } + String roleName = args[0]; + createLockRole(roleName); + } + + // snippet-start:[S3Lock.javav2.lock.role.main] + /** + * Creates an IAM role for AWS S3 Batch Operations to manage object locks. + */ + public static void createLockRole(String roleName) { + // Trust policy + final String trustPolicy = """ + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "Service": "batchoperations.s3.amazonaws.com" + }, + "Action": "sts:AssumeRole" + } + ] + } + """; + + + // Permissions policy + final String bopsPermissions = """ + { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": "s3:GetBucketObjectLockConfiguration", + "Resource": "arn:aws:s3:::amzn-s3-demo-manifest-bucket" + }, + { + "Effect": "Allow", + "Action": [ + "s3:GetObject", + "s3:GetObjectVersion", + "s3:GetBucketLocation" + ], + "Resource": "arn:aws:s3:::amzn-s3-demo-manifest-bucket/*" + }, + { + "Effect": "Allow", + "Action": [ + "s3:PutObject", + "s3:GetBucketLocation" + ], + "Resource": "arn:aws:s3:::amzn-s3-demo-completion-report-bucket/*" + } + ] + } + """; + + // Create IAM client + final IamClient iam = IamClient.builder() + .region(Region.US_WEST_2) + .build(); + + // Create the role with the trust policy + final CreateRoleRequest createRoleRequest = CreateRoleRequest.builder() + .assumeRolePolicyDocument(trustPolicy) + .roleName(roleName) + .build(); + + iam.createRole(createRoleRequest); + + // Attach the permissions policy to the role + final PutRolePolicyRequest putRolePolicyRequest = PutRolePolicyRequest.builder() + .policyDocument(bopsPermissions) + .policyName("batch_operations-permissions") + .roleName(roleName) + .build(); + + iam.putRolePolicy(putRolePolicyRequest); + System.out.println("The object lock role was created."); + } + // snippet-end:[S3Lock.javav2.lock.role.main] +}