use of software.amazon.awssdk.services.s3.model.PutObjectRetentionRequest in project aws-doc-sdk-examples by awsdocs.
the class PutObjectRetention method setRentionPeriod.
// snippet-start:[s3.java2.retention_object.main]
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 Amazon S3 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);
}
}
Aggregations