1+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+
5+ package com .example .s3 ;
6+
7+
8+ import software .amazon .awssdk .services .s3control .S3ControlClient ;
9+ import software .amazon .awssdk .services .s3control .model .*;
10+ import java .text .ParseException ;
11+ import java .text .SimpleDateFormat ;
12+ import java .util .Arrays ;
13+ import java .util .Date ;
14+
15+ // snippet-start:[s3.java2.create_governance_retemtion.main]
16+ /**
17+ * Before running this Java V2 code example, set up your development
18+ * environment, including your credentials.
19+ *
20+ * For more information, see the following documentation topic:
21+ *
22+ * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
23+ */
24+ public class CreateGovernanceRetentionJob {
25+
26+ public static void main (String []args ) throws ParseException {
27+ final String usage = """
28+
29+ Usage:
30+ <manifestObjectArn> <jobReportBucketArn> <roleArn> <accountId>
31+
32+ Where:
33+ manifestObjectArn - The Amazon Resource Name (ARN) of the S3 object that contains the manifest file for the governance objects.\s
34+ bucketName - The ARN of the S3 bucket where the job report will be stored.
35+ roleArn - The ARN of the IAM role that will be used to perform the governance retention operation.
36+ accountId - Your AWS account Id.
37+ """ ;
38+
39+ if (args .length != 4 ) {
40+ System .out .println (usage );
41+ return ;
42+ }
43+
44+ String manifestObjectArn = args [0 ];
45+ String jobReportBucketArn = args [1 ];
46+ String roleArn = args [2 ];
47+ String accountId = args [3 ];
48+
49+ S3ControlClient s3ControlClient = S3ControlClient .create ();
50+ createGovernanceRetentionJob (s3ControlClient , manifestObjectArn , jobReportBucketArn , roleArn , accountId );
51+ }
52+
53+ public static String createGovernanceRetentionJob (final S3ControlClient s3ControlClient , String manifestObjectArn , String jobReportBucketArn , String roleArn , String accountId ) throws ParseException {
54+ final String manifestObjectVersionId = "15ad5ba069e6bbc465c77bf83d541385" ;
55+ final JobManifestLocation manifestLocation = JobManifestLocation .builder ()
56+ .objectArn (manifestObjectArn )
57+ .eTag (manifestObjectVersionId )
58+ .build ();
59+
60+ final JobManifestSpec manifestSpec = JobManifestSpec .builder ()
61+ .format (JobManifestFormat .S3_BATCH_OPERATIONS_CSV_20180820 )
62+ .fields (Arrays .asList (JobManifestFieldName .BUCKET , JobManifestFieldName .KEY ))
63+ .build ();
64+
65+ final JobManifest manifestToPublicApi = JobManifest .builder ()
66+ .location (manifestLocation )
67+ .spec (manifestSpec )
68+ .build ();
69+
70+ final String jobReportPrefix = "reports/governance-objects" ;
71+ final JobReport jobReport = JobReport .builder ()
72+ .enabled (true )
73+ .reportScope (JobReportScope .ALL_TASKS )
74+ .bucket (jobReportBucketArn )
75+ .prefix (jobReportPrefix )
76+ .format (JobReportFormat .REPORT_CSV_20180820 )
77+ .build ();
78+
79+ final SimpleDateFormat format = new SimpleDateFormat ("dd/MM/yyyy" );
80+ final Date jan30th = format .parse ("30/01/2025" );
81+
82+ final S3SetObjectRetentionOperation s3SetObjectRetentionOperation = S3SetObjectRetentionOperation .builder ()
83+ .retention (S3Retention .builder ()
84+ .mode (S3ObjectLockRetentionMode .GOVERNANCE )
85+ .retainUntilDate (jan30th .toInstant ())
86+ .build ())
87+ .build ();
88+
89+ final JobOperation jobOperation = JobOperation .builder ()
90+ .s3PutObjectRetention (s3SetObjectRetentionOperation )
91+ .build ();
92+
93+ final Boolean requiresConfirmation = true ;
94+ final int priority = 10 ;
95+
96+ final CreateJobRequest request = CreateJobRequest .builder ()
97+ .accountId (accountId )
98+ .description ("Put governance retention" )
99+ .manifest (manifestToPublicApi )
100+ .operation (jobOperation )
101+ .priority (priority )
102+ .roleArn (roleArn )
103+ .report (jobReport )
104+ .confirmationRequired (requiresConfirmation )
105+ .build ();
106+
107+ final CreateJobResponse result = s3ControlClient .createJob (request );
108+ return result .jobId ();
109+ }
110+ }
111+ // snippet-end:[s3.java2.create_governance_retemtion.main]
0 commit comments