Si vous n'activez pas l'accès en temps réel aux objets de classe de stockage Archive, vous ne pouvez y accéder qu'après les avoir restaurés. Les classes de stockage Cold Archive et Deep Cold Archive ne prennent pas en charge l'accès en temps réel : vous devez impérativement restaurer ces objets avant d'y accéder. En règle générale, la restauration d'un objet Archive prend quelques minutes, celle d'un objet Cold Archive plusieurs heures, et celle d'un objet Deep Cold Archive entre 12 et 48 heures. Ces durées sont données à titre indicatif et peuvent varier selon le contexte. Cette rubrique explique comment restaurer des objets des classes de stockage Archive, Cold Archive et Deep Cold Archive.
Notes d'utilisation
L'opération RestoreObject s'applique uniquement aux objets des classes de stockage Archive, Cold Archive et Deep Cold Archive.
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, privilégiez un point de terminaison interne. Pour obtenir la liste des régions et des points de terminaison pris en charge, consultez Régions et points de terminaison.
Les identifiants d'accès utilisés dans cette rubrique sont récupérés depuis des variables d'environnement. Pour plus d'informations, reportez-vous à la section Configurer les informations d'identification d'accès.
Les exemples de cette rubrique illustrent la création d'une instance OSSClient à l'aide d'un endpoint OSS. Pour découvrir d'autres configurations, telles que l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants Security Token Service (STS), consultez la page Configuration du client.
La restauration d'un objet requiert l'autorisation
oss:RestoreObject. Pour en savoir plus, consultez la section Accorder une stratégie personnalisée.
Restaurer un objet Archive
L'exemple de code suivant montre comment restaurer un objet Archive :
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 Archive object. Do not include the bucket name in the full path.
String objectName = "exampledir/object";
// 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 {
ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);
// Check whether the object is an Archive object.
StorageClass storageClass = objectMetadata.getObjectStorageClass();
if (storageClass == StorageClass.Archive) {
// Restore the object.
ossClient.restoreObject(bucketName, objectName);
// Specify the time to wait for the object to be restored.
do {
Thread.sleep(1000);
objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);
} while (!objectMetadata.isRestoreCompleted());
}
// Query the restored object.
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
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();
}
}
}
}
Restaurer un objet Cold Archive
Le code ci-dessous illustre la procédure de restauration d'un objet Cold Archive :
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 Cold Archive object. Do not include the bucket name in the full path.
String objectName = "exampledir/object";
// 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 {
// Specify the restoration mode of the Cold Archive object.
// RestoreTier.RESTORE_TIER_EXPEDITED specifies that the object is restored within 1 hour.
// RestoreTier.RESTORE_TIER_STANDARD specifies that the object is restored within 2 to 5 hours.
// RestoreTier.RESTORE_TIER_BULK specifies that the object is restored within 5 to 12 hours.
RestoreJobParameters jobParameters = new RestoreJobParameters(RestoreTier.RESTORE_TIER_BULK);
// Configure parameters. For example, set the restoration mode of the object to Standard, and set the duration for which the object remains in the restored state to 2 days.
// The first parameter specifies the duration (days) for which the object remains in the restored state. The default value is 1. This parameter takes effect for Archive and Cold Archive objects.
// jobParameters specifies the restoration mode of the object and takes effect only for Cold Archive objects.
RestoreConfiguration configuration = new RestoreConfiguration(2, jobParameters);
// Initiate an object restoration request.
ossClient.restoreObject(bucketName, objectName, configuration);
} 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();
}
}
}
}
Restaurer un objet Deep Cold Archive
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 Deep Cold Archive object. Do not include the bucket name in the full path.
String objectName = "exampledir/object";
// 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 {
// Configure the restoration mode. In this example, the restoration mode is RESTORE_TIER_STANDARD.
// RestoreTier.RESTORE_TIER_EXPEDITED specifies that the object is restored within 12 hours.
// RestoreTier.RESTORE_TIER_STANDARD specifies that the object is restored within 48 hours.
RestoreJobParameters jobParameters = new RestoreJobParameters(RestoreTier.RESTORE_TIER_STANDARD);
// Specify the duration (days) for which the object remains in the restored state. The default value is 1. In this example, the validity period is 2 days.
RestoreConfiguration configuration = new RestoreConfiguration(2, jobParameters);
// Initiate an object restoration request.
ossClient.restoreObject(bucketName, objectName, configuration);
} 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();
}
}
}
}
Références
Pour consulter l'exemple de code complet dédié à la restauration d'objets, rendez-vous sur GitHub.
Pour plus de détails sur l'opération API permettant de restaurer des objets, voir RestoreObject.