PutObjectRetention.java - AWS Code Sample

PutObjectRetention.java

PutObjectRetention demonstrates how to place an object retention configuration on an object.

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.example.s3; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutObjectRetentionRequest; import software.amazon.awssdk.services.s3.model.ObjectLockRetention ; import software.amazon.awssdk.services.s3.model.S3Exception; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; public class PutObjectRetention { public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " PutObjectRetention <key> <bucket> \n\n" + "Where:\n" + " key - the name of the object (i.e., book.pdf)\n\n" + " bucket - the bucket name that contains the object (i.e., bucket1)\n" + "Example:\n" + " book.pdf bucket1\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1); } String key = args[0]; String bucket = args[1]; Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder() .region(region) .build(); setRentionPeriod(s3, key, bucket) ; } public static void setRentionPeriod(S3Client s3, String key, String bucket) { try{ LocalDate localDate = LocalDate.parse("2020-07-17"); LocalDateTime localDateTime = localDate.atStartOfDay(); Instant instant = localDateTime.toInstant(ZoneOffset.UTC); ObjectLockRetention lockRetention = ObjectLockRetention.builder() .mode("COMPLIANCE") .retainUntilDate(instant) .build(); PutObjectRetentionRequest retentionRequest = PutObjectRetentionRequest.builder() .bucket(bucket) .key(key) .bypassGovernanceRetention(true) .retention(lockRetention) .build(); /** * To set Retention on an object, the Bucket must support object locking, otherwise an exception is thrown */ s3.putObjectRetention(retentionRequest); System.out.print("An object retention configuration was successfully placed on the object"); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

Sample Details

Service: Amazon S3

Last tested: 7/13/2020

Author: scmacdon-aws

Type: full-example