Search in sources :

Example 1 with PutObjectRetentionRequest

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);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) PutObjectRetentionRequest(software.amazon.awssdk.services.s3.model.PutObjectRetentionRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) Instant(java.time.Instant) LocalDate(java.time.LocalDate) ObjectLockRetention(software.amazon.awssdk.services.s3.model.ObjectLockRetention)

Aggregations

Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 ObjectLockRetention (software.amazon.awssdk.services.s3.model.ObjectLockRetention)1 PutObjectRetentionRequest (software.amazon.awssdk.services.s3.model.PutObjectRetentionRequest)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1