Skip to content

Commit

Permalink
Java V2 Add a new S3Control example (#7105)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon authored Nov 26, 2024
1 parent aaa8b0b commit 3f7e820
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .doc_gen/metadata/s3-control_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ s3-control_CreateJob:
- s3control.java2.create_job.compliance.main
- description: Create a legal hold off job.
snippet_tags:
- s3control.java2.create_job.compliance.main
- s3control.java2.create_job.compliance.main
- description: Create a new governance retemtion job.
snippet_tags:
- s3.java2.create_governance_retemtion.main
services:
s3-control: {CreateJob}
s3-control_PutJobTagging:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0


package com.example.s3;


import software.amazon.awssdk.services.s3control.S3ControlClient;
import software.amazon.awssdk.services.s3control.model.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

// snippet-start:[s3.java2.create_governance_retemtion.main]
/**
* 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 CreateGovernanceRetentionJob {

public static void main(String[]args) throws ParseException {
final String usage = """
Usage:
<manifestObjectArn> <jobReportBucketArn> <roleArn> <accountId> <manifestObjectVersionId>
Where:
manifestObjectArn - The Amazon Resource Name (ARN) of the S3 object that contains the manifest file for the governance objects.\s
bucketName - The ARN of the S3 bucket where the job report will be stored.
roleArn - The ARN of the IAM role that will be used to perform the governance retention operation.
accountId - Your AWS account Id.
manifestObjectVersionId = A unique value that is used as the `eTag` property of the `JobManifestLocation` object.
""";

if (args.length != 4) {
System.out.println(usage);
return;
}

String manifestObjectArn = args[0];
String jobReportBucketArn = args[1];
String roleArn = args[2];
String accountId = args[3];
String manifestObjectVersionId = args[4];

S3ControlClient s3ControlClient = S3ControlClient.create();
createGovernanceRetentionJob(s3ControlClient, manifestObjectArn, jobReportBucketArn, roleArn, accountId, manifestObjectVersionId);
}

public static String createGovernanceRetentionJob(final S3ControlClient s3ControlClient, String manifestObjectArn, String jobReportBucketArn, String roleArn, String accountId, String manifestObjectVersionId) throws ParseException {
final JobManifestLocation manifestLocation = JobManifestLocation.builder()
.objectArn(manifestObjectArn)
.eTag(manifestObjectVersionId)
.build();

final JobManifestSpec manifestSpec = JobManifestSpec.builder()
.format(JobManifestFormat.S3_BATCH_OPERATIONS_CSV_20180820)
.fields(Arrays.asList(JobManifestFieldName.BUCKET, JobManifestFieldName.KEY))
.build();

final JobManifest manifestToPublicApi = JobManifest.builder()
.location(manifestLocation)
.spec(manifestSpec)
.build();

final String jobReportPrefix = "reports/governance-objects";
final JobReport jobReport = JobReport.builder()
.enabled(true)
.reportScope(JobReportScope.ALL_TASKS)
.bucket(jobReportBucketArn)
.prefix(jobReportPrefix)
.format(JobReportFormat.REPORT_CSV_20180820)
.build();

final SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
final Date jan30th = format.parse("30/01/2025");

final S3SetObjectRetentionOperation s3SetObjectRetentionOperation = S3SetObjectRetentionOperation.builder()
.retention(S3Retention.builder()
.mode(S3ObjectLockRetentionMode.GOVERNANCE)
.retainUntilDate(jan30th.toInstant())
.build())
.build();

final JobOperation jobOperation = JobOperation.builder()
.s3PutObjectRetention(s3SetObjectRetentionOperation)
.build();

final Boolean requiresConfirmation = true;
final int priority = 10;

final CreateJobRequest request = CreateJobRequest.builder()
.accountId(accountId)
.description("Put governance retention")
.manifest(manifestToPublicApi)
.operation(jobOperation)
.priority(priority)
.roleArn(roleArn)
.report(jobReport)
.confirmationRequired(requiresConfirmation)
.build();

final CreateJobResponse result = s3ControlClient.createJob(request);
return result.jobId();
}
}
// snippet-end:[s3.java2.create_governance_retemtion.main]
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public static void main(String[] args) {
*/
public static void setLifecycleConfig(S3Client s3, String bucketName, String accountId) {
try {
// Create a rule to archive objects with the "glacierobjects/" prefix to Amazon
// S3 Glacier.
// Create a rule to archive objects with the "glacierobjects/" prefix to the
// S3 Glacier Flexible Retrieval storage class immediately.
LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder()
.prefix("glacierobjects/")
.build();
Expand Down

0 comments on commit 3f7e820

Please sign in to comment.