-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a new IAM example to support S3 topic
- Loading branch information
Showing
2 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
javav2/example_code/iam/src/main/java/com/example/iam/CreateObjectLockRole.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.example.iam; | ||
|
||
import software.amazon.awssdk.services.iam.IamClient; | ||
import software.amazon.awssdk.services.iam.model.CreateRoleRequest; | ||
import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest; | ||
|
||
/** | ||
* Before running this Java V2 code example, set up your development | ||
* environment, including your credentials. | ||
* | ||
* For more information, see the following documentation topic: | ||
* | ||
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html | ||
*/ | ||
public class CreateObjectLockRole { | ||
|
||
public static void main(String[] args) { | ||
final String roleName = "<Enter role name>"; | ||
IamClient iam = IamClient.builder().build(); | ||
createObjectLockRole(iam, roleName); | ||
} | ||
|
||
// snippet-start:[iam.java2.s3_role.main] | ||
/** | ||
* Creates an IAM role with the necessary permissions to perform object lock operations on an S3 bucket. | ||
* | ||
* @param iam An instance of the {@link IamClient} class, which is used to interact with the AWS IAM service. | ||
* @param roleName The name of the IAM role to be created. | ||
*/ | ||
public static void createObjectLockRole(IamClient iam, String roleName) { | ||
final String bopsPermissions = """ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": "s3:GetBucketObjectLockConfiguration", | ||
"Resource": [ | ||
"arn:aws:s3:::<ENTER Bucket Name>" | ||
] | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:GetObject", | ||
"s3:GetObjectVersion", | ||
"s3:GetBucketLocation" | ||
], | ||
"Resource": [ | ||
"arn:aws:s3:::<ENTER Bucket Name>/*" | ||
] | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:PutObject", | ||
"s3:GetBucketLocation" | ||
], | ||
"Resource": [ | ||
"arn:aws:s3:::<ENTER Bucket Name>/*" | ||
] | ||
} | ||
] | ||
}"""; | ||
|
||
CreateRoleRequest createRoleRequest = CreateRoleRequest.builder() | ||
.assumeRolePolicyDocument(bopsPermissions) | ||
.roleName(roleName) | ||
.build(); | ||
|
||
iam.createRole(createRoleRequest); | ||
PutRolePolicyRequest putRolePolicyRequest = PutRolePolicyRequest.builder() | ||
.policyDocument(bopsPermissions) | ||
.policyName("batch_operations-permissions") | ||
.roleName(roleName) | ||
.build(); | ||
|
||
iam.putRolePolicy(putRolePolicyRequest); | ||
} | ||
// snippet-end:[iam.java2.s3_role.main] | ||
} |