Restore an object (Java SDK V2)

Updated at:

Restore an archived object of a specific version from a versioning-enabled bucket by specifying the version ID with OSS Java SDK V2.

Prerequisites

  • OSS Java SDK V2 is installed. If you use Maven, add the com.aliyun:alibabacloud-oss-v2 dependency to 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

An Alibaba Cloud account has all permissions by default. RAM users and RAM roles under an Alibaba Cloud account have no permissions by default. The Alibaba Cloud account or an account administrator must grant the required permissions to the RAM users or RAM roles.

API

Action

Description

RestoreObject

oss:RestoreObject

Restores the specified version of an archived object by version ID.

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

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

        try (OSSClient client = clientBuilder.build()) {

            // Specify versionId to restore a specific version of the archived object
            RestoreObjectResult result = client.restoreObject(RestoreObjectRequest.newBuilder()
                    .bucket(bucket)
                    .key(key)
                    .versionId(versionId)
                    .build());

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

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

References