Skip to content

Commit ba41802

Browse files
committed
add a new IAM example to support S3 topic
1 parent 5b97dba commit ba41802

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

.doc_gen/metadata/iam_metadata.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1706,9 +1706,12 @@ iam_CreateRole:
17061706
github: javav2/example_code/iam
17071707
sdkguide:
17081708
excerpts:
1709-
- description:
1709+
- description: Creates an IAM role.
17101710
snippet_tags:
17111711
- iam.java2.create_role.main
1712+
- description: Creates an IAM role with the necessary permissions to perform object lock operations on an S3 bucket.
1713+
snippet_tags:
1714+
- iam.java2.s3_role.main
17121715
PHP:
17131716
versions:
17141717
- sdk_version: 3
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.iam;
5+
6+
import software.amazon.awssdk.services.iam.IamClient;
7+
import software.amazon.awssdk.services.iam.model.CreateRoleRequest;
8+
import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest;
9+
10+
/**
11+
* Before running this Java V2 code example, set up your development
12+
* environment, including your credentials.
13+
*
14+
* For more information, see the following documentation topic:
15+
*
16+
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
17+
*/
18+
public class CreateObjectLockRole {
19+
20+
public static void main(String[] args) {
21+
final String roleName = "<Enter role name>";
22+
IamClient iam = IamClient.builder().build();
23+
createObjectLockRole(iam, roleName);
24+
}
25+
26+
// snippet-start:[iam.java2.s3_role.main]
27+
/**
28+
* Creates an IAM role with the necessary permissions to perform object lock operations on an S3 bucket.
29+
*
30+
* @param iam An instance of the {@link IamClient} class, which is used to interact with the AWS IAM service.
31+
* @param roleName The name of the IAM role to be created.
32+
*/
33+
public static void createObjectLockRole(IamClient iam, String roleName) {
34+
final String bopsPermissions = """
35+
{
36+
"Version": "2012-10-17",
37+
"Statement": [
38+
{
39+
"Effect": "Allow",
40+
"Action": "s3:GetBucketObjectLockConfiguration",
41+
"Resource": [
42+
"arn:aws:s3:::<ENTER Bucket Name>"
43+
]
44+
},
45+
{
46+
"Effect": "Allow",
47+
"Action": [
48+
"s3:GetObject",
49+
"s3:GetObjectVersion",
50+
"s3:GetBucketLocation"
51+
],
52+
"Resource": [
53+
"arn:aws:s3:::<ENTER Bucket Name>/*"
54+
]
55+
},
56+
{
57+
"Effect": "Allow",
58+
"Action": [
59+
"s3:PutObject",
60+
"s3:GetBucketLocation"
61+
],
62+
"Resource": [
63+
"arn:aws:s3:::<ENTER Bucket Name>/*"
64+
]
65+
}
66+
]
67+
}""";
68+
69+
CreateRoleRequest createRoleRequest = CreateRoleRequest.builder()
70+
.assumeRolePolicyDocument(bopsPermissions)
71+
.roleName(roleName)
72+
.build();
73+
74+
iam.createRole(createRoleRequest);
75+
PutRolePolicyRequest putRolePolicyRequest = PutRolePolicyRequest.builder()
76+
.policyDocument(bopsPermissions)
77+
.policyName("batch_operations-permissions")
78+
.roleName(roleName)
79+
.build();
80+
81+
iam.putRolePolicy(putRolePolicyRequest);
82+
}
83+
// snippet-end:[iam.java2.s3_role.main]
84+
}

0 commit comments

Comments
 (0)