diff --git a/.doc_gen/metadata/s3-control_metadata.yaml b/.doc_gen/metadata/s3-control_metadata.yaml index 73622adfd55..dc082a299df 100644 --- a/.doc_gen/metadata/s3-control_metadata.yaml +++ b/.doc_gen/metadata/s3-control_metadata.yaml @@ -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: diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/CreateGovernanceRetentionJob.java b/javav2/example_code/s3/src/main/java/com/example/s3/CreateGovernanceRetentionJob.java new file mode 100644 index 00000000000..27bed92bb4f --- /dev/null +++ b/javav2/example_code/s3/src/main/java/com/example/s3/CreateGovernanceRetentionJob.java @@ -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: + + + 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] \ No newline at end of file diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java b/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java index 87a323465d4..4e7f0fc2c47 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java @@ -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();