Object-level retention (Object Worm) (Java SDK V2)

Updated at:

Object Worm enforces WORM (write once, read many) retention at the object level: each object gets its own retention mode and retain-until date, and can also be placed under legal hold. The following sections show how to use OSS SDK for Java V2 to enable Object Worm, query its configuration, set and query object retention, and manage legal hold.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou in the China (Hangzhou) region and the public endpoint by default. To access OSS from other Alibaba Cloud services deployed in the same region, use the internal endpoint instead. For the mapping between OSS regions and endpoints, see Regions and endpoints.

Sample code

Enable Object Worm and configure the default retention policy

Use the following code to enable Object Worm and configure the default retention policy:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;

public class PutBucketObjectWormConfigurationDemo {

    public static void main(String[] args) throws Exception {

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClient client = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region("cn-hangzhou")
                .build();

        try {
            ObjectWormConfigurationDefaultRetention defaultRetention = ObjectWormConfigurationDefaultRetention.newBuilder()
                    .mode("COMPLIANCE")
                    .days(1)
                    .build();

            ObjectWormConfigurationRule rule = ObjectWormConfigurationRule.newBuilder()
                    .defaultRetention(defaultRetention)
                    .build();

            ObjectWormConfiguration objectWormConfiguration = ObjectWormConfiguration.newBuilder()
                    .objectWormEnabled("Enabled")
                    .rule(rule)
                    .build();

            PutBucketObjectWormConfigurationRequest request = PutBucketObjectWormConfigurationRequest.newBuilder()
                    .bucket("examplebucket")
                    .objectWormConfiguration(objectWormConfiguration)
                    .build();

            PutBucketObjectWormConfigurationResult result = client.putBucketObjectWormConfiguration(request);

            System.out.printf("Status code:%d, request id:%s%n",
                    result.statusCode(), result.requestId());

        } catch (Exception e) {
            System.out.printf("error: %s%n", e);
        } finally {
            client.close();
        }
    }
}

Query Object Worm configuration

Use the following code to query the Object Worm configuration of a bucket:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;

public class GetBucketObjectWormConfigurationDemo {

    public static void main(String[] args) throws Exception {

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClient client = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region("cn-hangzhou")
                .build();

        try {
            GetBucketObjectWormConfigurationRequest request = GetBucketObjectWormConfigurationRequest.newBuilder()
                    .bucket("examplebucket")
                    .build();

            GetBucketObjectWormConfigurationResult result = client.getBucketObjectWormConfiguration(request);

            System.out.printf("Status code:%d, request id:%s%n",
                    result.statusCode(), result.requestId());

            if (result.objectWormConfiguration() != null) {
                System.out.printf("Object Worm Enabled: %s%n", result.objectWormConfiguration().objectWormEnabled());
                if (result.objectWormConfiguration().rule() != null &&
                    result.objectWormConfiguration().rule().defaultRetention() != null) {
                    ObjectWormConfigurationDefaultRetention retention = result.objectWormConfiguration().rule().defaultRetention();
                    System.out.printf("Retention Mode: %s%n", retention.mode());
                    System.out.printf("Retention Days: %s%n", retention.days());
                    System.out.printf("Retention Years: %s%n", retention.years());
                }
            }

        } catch (Exception e) {
            System.out.printf("error: %s%n", e);
        } finally {
            client.close();
        }
    }
}

Set object retention

Use the following code to set a separate retention policy for an object:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;

public class PutObjectRetentionDemo {

    public static void main(String[] args) throws Exception {

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClient client = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region("cn-hangzhou")
                .build();

        try {
            Retention retention = Retention.newBuilder()
                    .mode("COMPLIANCE")
                    .retainUntilDate("2026-12-31T00:00:00.000Z")
                    .build();

            PutObjectRetentionRequest request = PutObjectRetentionRequest.newBuilder()
                    .bucket("examplebucket")
                    .key("exampleobject")
                    .retention(retention)
                    .build();

            PutObjectRetentionResult result = client.putObjectRetention(request);

            System.out.printf("Status code:%d, request id:%s%n",
                    result.statusCode(), result.requestId());

        } catch (Exception e) {
            System.out.printf("error: %s%n", e);
        } finally {
            client.close();
        }
    }
}

Query object retention

Use the following code to query the retention policy of an object:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;

public class GetObjectRetentionDemo {

    public static void main(String[] args) throws Exception {

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClient client = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region("cn-hangzhou")
                .build();

        try {
            GetObjectRetentionRequest request = GetObjectRetentionRequest.newBuilder()
                    .bucket("examplebucket")
                    .key("exampleobject")
                    .build();

            GetObjectRetentionResult result = client.getObjectRetention(request);

            System.out.printf("Status code:%d, request id:%s%n",
                    result.statusCode(), result.requestId());

            if (result.retention() != null) {
                System.out.printf("Retention Mode: %s%n", result.retention().mode());
                System.out.printf("Retain Until Date: %s%n", result.retention().retainUntilDate());
            }

        } catch (Exception e) {
            System.out.printf("error: %s%n", e);
        } finally {
            client.close();
        }
    }
}

Set and query legal hold

A legal hold keeps an object under indefinite, object-level retention: while the hold is in effect, the object can't be deleted or overwritten. Use legal holds when the retention duration can't be determined in advance, such as during a legal investigation. Object Worm must be enabled on the bucket before you set a legal hold. Use the following code to set a legal hold on an object:

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;

public class PutObjectLegalHoldDemo {

    public static void main(String[] args) throws Exception {

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClient client = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region("cn-hangzhou")
                .build();

        try {
            // status: ON enables the legal hold, and OFF removes it.
            LegalHold legalHold = LegalHold.newBuilder()
                    .status("ON")
                    .build();

            PutObjectLegalHoldRequest request = PutObjectLegalHoldRequest.newBuilder()
                    .bucket("examplebucket")
                    .key("exampleobject")
                    .legalHold(legalHold)
                    .build();

            PutObjectLegalHoldResult result = client.putObjectLegalHold(request);

            System.out.printf("Status code:%d, request id:%s%n",
                    result.statusCode(), result.requestId());

        } catch (Exception e) {
            System.out.printf("error: %s%n", e);
        } finally {
            client.close();
        }
    }
}

To query the legal hold status of an object, remove the legalHold parameter from the preceding request and call client.getObjectLegalHold(GetObjectLegalHoldRequest.newBuilder().bucket(bucket).key(key).build()). Read the status (ON or OFF) from legalHold().status() in the response.

References