Compliance retention policies (Java SDK V2)

Updated at:

Manage compliance retention policies by calling the InitiateBucketWorm, GetBucketWorm, and CompleteBucketWorm operations with OSS Java SDK V2.

Prerequisites

  • OSS Java SDK V2 is installed. If you use Maven, add the dependency com.aliyun:alibabacloud-oss-v2 to your pom.xml.

  • Access credentials are configured by using environment variables. The sample code in this topic uses EnvironmentVariableCredentialsProvider to read credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

Permissions

By default, an Alibaba Cloud account has all permissions, but RAM users and RAM roles under the account have none. The Alibaba Cloud account or an account administrator must grant the permissions required to perform the operations.

API

Action

Description

InitiateBucketWorm

oss:InitiateBucketWorm

Create, query, or lock compliance retention policies.

Sample code

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 BucketWormSample {
    public static void main(String[] args) throws Exception {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            // Create a compliance retention policy (retention period: 1 day)
            InitiateWormConfiguration wormConfig = InitiateWormConfiguration.newBuilder()
                    .retentionPeriodInDays(1)
                    .build();

            InitiateBucketWormResult initResult = client.initiateBucketWorm(
                    InitiateBucketWormRequest.newBuilder()
                            .bucket(bucket)
                            .initiateWormConfiguration(wormConfig)
                            .build());

            String wormId = initResult.wormId();
            System.out.printf("InitiateBucketWorm worm id:%s%n", wormId);

            // Query the compliance retention policy
            GetBucketWormResult getResult = client.getBucketWorm(
                    GetBucketWormRequest.newBuilder().bucket(bucket).build());

            WormConfiguration config = getResult.wormConfiguration();
            System.out.printf("Worm ID: %s, State: %s, Retention days: %d%n",
                    config.wormId(), config.state(), config.retentionPeriodInDays());

            // Lock the compliance retention policy
            CompleteBucketWormResult completeResult = client.completeBucketWorm(
                    CompleteBucketWormRequest.newBuilder()
                            .bucket(bucket)
                            .wormId(wormId)
                            .build());

            System.out.printf("CompleteBucketWorm status code:%d%n", completeResult.statusCode());

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

References