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> <manifestObjectVersionId>
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+ manifestObjectVersionId = A unique value that is used as the `eTag` property of the `JobManifestLocation` object.
38+ """ ;
39+
40+ if (args .length != 4 ) {
41+ System .out .println (usage );
42+ return ;
43+ }
44+
45+ String manifestObjectArn = args [0 ];
46+ String jobReportBucketArn = args [1 ];
47+ String roleArn = args [2 ];
48+ String accountId = args [3 ];
49+ String manifestObjectVersionId = args [4 ];
50+
51+ S3ControlClient s3ControlClient = S3ControlClient .create ();
52+ createGovernanceRetentionJob (s3ControlClient , manifestObjectArn , jobReportBucketArn , roleArn , accountId , manifestObjectVersionId );
53+ }
54+
55+ public static String createGovernanceRetentionJob (final S3ControlClient s3ControlClient , String manifestObjectArn , String jobReportBucketArn , String roleArn , String accountId , String manifestObjectVersionId ) throws ParseException {
56+ final JobManifestLocation manifestLocation = JobManifestLocation .builder ()
57+ .objectArn (manifestObjectArn )
58+ .eTag (manifestObjectVersionId )
59+ .build ();
60+
61+ final JobManifestSpec manifestSpec = JobManifestSpec .builder ()
62+ .format (JobManifestFormat .S3_BATCH_OPERATIONS_CSV_20180820 )
63+ .fields (Arrays .asList (JobManifestFieldName .BUCKET , JobManifestFieldName .KEY ))
64+ .build ();
65+
66+ final JobManifest manifestToPublicApi = JobManifest .builder ()
67+ .location (manifestLocation )
68+ .spec (manifestSpec )
69+ .build ();
70+
71+ final String jobReportPrefix = "reports/governance-objects" ;
72+ final JobReport jobReport = JobReport .builder ()
73+ .enabled (true )
74+ .reportScope (JobReportScope .ALL_TASKS )
75+ .bucket (jobReportBucketArn )
76+ .prefix (jobReportPrefix )
77+ .format (JobReportFormat .REPORT_CSV_20180820 )
78+ .build ();
79+
80+ final SimpleDateFormat format = new SimpleDateFormat ("dd/MM/yyyy" );
81+ final Date jan30th = format .parse ("30/01/2025" );
82+
83+ final S3SetObjectRetentionOperation s3SetObjectRetentionOperation = S3SetObjectRetentionOperation .builder ()
84+ .retention (S3Retention .builder ()
85+ .mode (S3ObjectLockRetentionMode .GOVERNANCE )
86+ .retainUntilDate (jan30th .toInstant ())
87+ .build ())
88+ .build ();
89+
90+ final JobOperation jobOperation = JobOperation .builder ()
91+ .s3PutObjectRetention (s3SetObjectRetentionOperation )
92+ .build ();
93+
94+ final Boolean requiresConfirmation = true ;
95+ final int priority = 10 ;
96+
97+ final CreateJobRequest request = CreateJobRequest .builder ()
98+ .accountId (accountId )
99+ .description ("Put governance retention" )
100+ .manifest (manifestToPublicApi )
101+ .operation (jobOperation )
102+ .priority (priority )
103+ .roleArn (roleArn )
104+ .report (jobReport )
105+ .confirmationRequired (requiresConfirmation )
106+ .build ();
107+
108+ final CreateJobResponse result = s3ControlClient .createJob (request );
109+ return result .jobId ();
110+ }
111+ }
112+ // snippet-end:[s3.java2.create_governance_retemtion.main]
0 commit comments