Restore objects (OSS SDK for Java 1.0)
In a versioned bucket, each object version can have a different storage class. The RestoreObject operation restores the current version by default. Specify a version ID to restore a specific version.
Usage notes
-
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
-
In this topic, access credentials are obtained from environment variables. For more information, see Configure access credentials.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Client configuration.
-
To restore an object, you must have the
oss:RestoreObjectpermission. For more information, see Grant a custom policy.
Examples
The following code restores an object from Archive, Cold Archive, or Deep Cold Archive storage:
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path.
String objectName = "exampledir/object";
// Specify the version ID of the Archive, Cold Archive or Deep Cold Archive object that you want to restore.
String versionId = "CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDQzOGY5NTkyNTI3MGYyMzJm****";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
String region = "cn-hangzhou";
// Create an OSSClient instance.
// Call the shutdown method to release resources when the OSSClient is no longer in use.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Query part of metadata of the specified version of the object.
GenericRequest getObjectMetadataRequest = new GenericRequest(bucketName,objectName, versionId);
ObjectMetadata objectMetadata = ossClient.getObjectMetadata(getObjectMetadataRequest);
// Check whether the specified version of the object is an Archive, a Cold Archive or a Deep Cold Archive object.
StorageClass storageClass = objectMetadata.getObjectStorageClass();
System.out.println("object storage class:" + objectMetadata.getObjectStorageClass());
if (storageClass == StorageClass.Archive) {
// Restore the object.
GenericRequest genericRequest = new GenericRequest(bucketName, objectName, versionId);
RestoreObjectResult restoreObjectResult = ossClient.restoreObject(genericRequest);
System.out.println("restor versionid: " + restoreObjectResult.getVersionId());
// Specify the time to wait for the object to be restored.
do {
Thread.sleep(1000);
objectMetadata = ossClient.getObjectMetadata(getObjectMetadataRequest);
System.out.println("is competed:" + objectMetadata.isRestoreCompleted());
} while (!objectMetadata.isRestoreCompleted());
}
// Specify the version ID of the object that you want to restore.
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);
getObjectRequest.setVersionId(versionId);
OSSObject ossObject = ossClient.getObject(getObjectRequest);
ossObject.getObjectContent().close();
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
Reference
For more information about the API operation that you can call to restore an object, see RestoreObject.