Skip to content

Commit a51baf9

Browse files
authored
Java V2 update the S3 Java ListBuckets example (#7192)
1 parent b2d0130 commit a51baf9

File tree

4 files changed

+115
-14
lines changed

4 files changed

+115
-14
lines changed

.doc_gen/metadata/s3-control_metadata.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ s3-control_CreateJob:
3333
- description: Create a legal hold off job.
3434
snippet_tags:
3535
- s3control.java2.create_job.compliance.main
36-
- description: Create a new governance retemtion job.
36+
- description: Create a new governance retention job.
3737
snippet_tags:
3838
- s3.java2.create_governance_retemtion.main
3939
services:

javav2/example_code/s3/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</execution>
4949
</executions>
5050
</plugin>
51-
</plugins>
51+
</plugins>
5252
</build>
5353
<dependencyManagement>
5454
<dependencies>
@@ -185,4 +185,4 @@
185185
<version>2.10.1</version>
186186
</dependency>
187187
</dependencies>
188-
</project>
188+
</project>

javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
// snippet-start:[s3.java2.list.buckets.main]
77
import software.amazon.awssdk.regions.Region;
88
import software.amazon.awssdk.services.s3.S3Client;
9-
import software.amazon.awssdk.services.s3.model.Bucket;
10-
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
11-
import java.util.List;
12-
9+
import software.amazon.awssdk.services.s3.paginators.ListBucketsIterable;
1310
/**
1411
* Before running this Java V2 code example, set up your development
1512
* environment, including your credentials.
@@ -35,11 +32,8 @@ public static void main(String[] args) {
3532
* @param s3 The {@link S3Client} instance to use for interacting with the Amazon S3 service.
3633
*/
3734
public static void listAllBuckets(S3Client s3) {
38-
ListBucketsResponse response = s3.listBuckets();
39-
List<Bucket> bucketList = response.buckets();
40-
for (Bucket bucket: bucketList) {
41-
System.out.println("Bucket name "+bucket.name());
42-
}
35+
ListBucketsIterable response = s3.listBucketsPaginator();
36+
response.buckets().forEach(bucket ->
37+
System.out.println("Bucket Name: " + bucket.name()));
4338
}
44-
}
45-
// snippet-end:[s3.java2.list.buckets.main]
39+
}// snippet-end:[s3.java2.list.buckets.main]
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
5+
package com.example.s3.batch;
6+
7+
import software.amazon.awssdk.regions.Region;
8+
import software.amazon.awssdk.services.iam.IamClient;
9+
import software.amazon.awssdk.services.iam.model.CreateRoleRequest;
10+
import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest;
11+
12+
public class CreateObjectLockRole {
13+
public static void main(String[] args) {
14+
final String usage = """
15+
16+
Usage: <roleName>
17+
18+
Where:
19+
roleName - the IAM role name.
20+
""";
21+
22+
if (args.length != 1) {
23+
System.out.println(usage);
24+
System.exit(1);
25+
}
26+
String roleName = args[0];
27+
createLockRole(roleName);
28+
}
29+
30+
// snippet-start:[S3Lock.javav2.lock.role.main]
31+
/**
32+
* Creates an IAM role for AWS S3 Batch Operations to manage object locks.
33+
*/
34+
public static void createLockRole(String roleName) {
35+
// Trust policy
36+
final String trustPolicy = """
37+
{
38+
"Version": "2012-10-17",
39+
"Statement": [
40+
{
41+
"Effect": "Allow",
42+
"Principal": {
43+
"Service": "batchoperations.s3.amazonaws.com"
44+
},
45+
"Action": "sts:AssumeRole"
46+
}
47+
]
48+
}
49+
""";
50+
51+
52+
// Permissions policy
53+
final String bopsPermissions = """
54+
{
55+
"Version": "2012-10-17",
56+
"Statement": [
57+
{
58+
"Effect": "Allow",
59+
"Action": "s3:GetBucketObjectLockConfiguration",
60+
"Resource": "arn:aws:s3:::amzn-s3-demo-manifest-bucket"
61+
},
62+
{
63+
"Effect": "Allow",
64+
"Action": [
65+
"s3:GetObject",
66+
"s3:GetObjectVersion",
67+
"s3:GetBucketLocation"
68+
],
69+
"Resource": "arn:aws:s3:::amzn-s3-demo-manifest-bucket/*"
70+
},
71+
{
72+
"Effect": "Allow",
73+
"Action": [
74+
"s3:PutObject",
75+
"s3:GetBucketLocation"
76+
],
77+
"Resource": "arn:aws:s3:::amzn-s3-demo-completion-report-bucket/*"
78+
}
79+
]
80+
}
81+
""";
82+
83+
// Create IAM client
84+
final IamClient iam = IamClient.builder()
85+
.region(Region.US_WEST_2)
86+
.build();
87+
88+
// Create the role with the trust policy
89+
final CreateRoleRequest createRoleRequest = CreateRoleRequest.builder()
90+
.assumeRolePolicyDocument(trustPolicy)
91+
.roleName(roleName)
92+
.build();
93+
94+
iam.createRole(createRoleRequest);
95+
96+
// Attach the permissions policy to the role
97+
final PutRolePolicyRequest putRolePolicyRequest = PutRolePolicyRequest.builder()
98+
.policyDocument(bopsPermissions)
99+
.policyName("batch_operations-permissions")
100+
.roleName(roleName)
101+
.build();
102+
103+
iam.putRolePolicy(putRolePolicyRequest);
104+
System.out.println("The object lock role was created.");
105+
}
106+
// snippet-end:[S3Lock.javav2.lock.role.main]
107+
}

0 commit comments

Comments
 (0)