All Products
Search
Document Center

Object Storage Service:Restore objects

Last Updated:Mar 11, 2024

You need to restore an Archive object or a Cold Archive object before you can read the object. This topic describes how to restore an Archive object or a Cold Archive object.

Usage notes

  • You can call the RestoreObject operation to restore only Archive and Cold Archive objects.

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • To restore objects, you must have the oss:RestoreObject permission. For more information, see Attach a custom policy to a RAM user.

Restore an Archive object

The following sample code provides an example on how to restore an Archive object:

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// 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. 
$provider = new EnvironmentVariableCredentialsProvider();
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the Archive object. Do not include the bucket name in the full path. Example: srcexampleobject.txt. 
$object = "srcexampleobject.txt";

try {
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);
    // Restore the object. 
    $ossClient->restoreObject($bucket, $object);
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . ": OK" . "\n");        

Restore a Cold Archive object

The following sample code provides an example on how to restore a Cold Archive object:

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;
use OSS\Model\RestoreConfig;

// 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. 
$provider = new EnvironmentVariableCredentialsProvider();
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: srcexampleobject.txt. 
$object = "srcexampleobject.txt";

try {
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);
    // Specify the duration in which the object remains in the restored state. Unit: days. Valid values: 1 to 7. 
    $day = 3; 
    // Specify the restoration priority of the object. In this example, this parameter is set to Expedited, which specifies that the object is restored within 1 hour. 
    // If you want to restore the object within 2 to 5 hours, set this parameter to Standard. If you want to restore the object within 5 to 12 hours, set this parameter to Bulk. 
    $tier = "Expedited"; 

    $config = new RestoreConfig($day,$tier);

    $options = array(
      OssClient::OSS_RESTORE_CONFIG => $config
    );

        $result = $ossClient->restoreObject($bucket, $object,$options);
} catch (OssException $e) {
    printf($e->getMessage() . "\n");
    return;
}            

References

For more information about the API operation that you can call to restore Archive and Cold Archive objects, see RestoreObject.