Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java V2 update the S3 Java ListBuckets example #7192

Merged
merged 10 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .doc_gen/metadata/s3-control_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions javav2/example_code/s3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</execution>
</executions>
</plugin>
</plugins>
</plugins>
</build>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -185,4 +185,4 @@
<version>2.10.1</version>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Bucket> 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]
}// snippet-end:[s3.java2.list.buckets.main]
Original file line number Diff line number Diff line change
@@ -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: <roleName>

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]
}
Loading