All Products
Search
Document Center

Object Storage Service:Restore objects (PHP SDK V2)

Last Updated:Sep 25, 2025

Objects in the Archive, Cold Archive, and Deep Cold Archive storage classes must be restored before they can be read. When an object is restored, a temporary replica in the Standard storage class is created. The original object and its replica coexist. The replica is automatically deleted after the restoration period expires. This topic describes how to restore Archive, Cold Archive, and Deep Cold Archive objects using the OSS SDK for PHP.

Notes

  • You can call the RestoreObject operation to restore only objects in the Archive, Cold Archive, and Deep Cold Archive storage classes.

  • The sample code in this topic uses the region ID cn-hangzhou for the China (Hangzhou) region. A public endpoint is used by default. If you want to access OSS from other Alibaba Cloud services in the same region, you can use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.

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

Sample code

Use the following code to restore Archive, Cold Archive, or Deep Cold Archive objects:

<?php

// Include the autoload file to ensure that dependency libraries are correctly loaded.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the descriptions of command-line parameters.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // Required. The region in which the bucket is located.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // Optional. The endpoint.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // Required. The name of the bucket.
    "key" => ['help' => 'The name of the object', 'required' => True], // Required. The name of the object.
];

// Convert the parameter descriptions to the long options format that is required by getopt.
// A colon (:) is added to the end of each parameter to indicate that the parameter requires a value.
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse the command-line parameters.
$options = getopt("", $longopts);

// Check whether required parameters exist.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help']; // Obtain the help information for the parameter.
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1); // Exit the program if a required parameter is missing.
    }
}

// Extract values from the parsed parameters.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$key = $options["key"];       // The name of the object.

// Load the credential information from environment variables.
// Use EnvironmentVariableCredentialsProvider to read the Access Key ID and Access Key Secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider.
$cfg->setRegion($region); // Set the region in which the bucket is located.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]); // Set the endpoint if an endpoint is provided.
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Create a RestoreObjectRequest object to restore an Archive object.
$request = new Oss\Models\RestoreObjectRequest(bucket: $bucket, key: $key);

// Restore the object.
$result = $client->restoreObject($request);

// Print the restoration result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, 200 indicates success.
    'request id:' . $result->requestId . PHP_EOL     // The request ID, which is used to debug or trace the request.
);

Common scenarios

Delete a replica using the CleanRestoredObject method

When you restore a Cold Archive or Deep Cold Archive object, a temporary replica in the Standard storage class is created for access. You are charged for the storage of this replica until the restoration period expires. To end the restoration early and stop incurring storage fees for the replica, you can send a CleanRestoredObject request. After the request is complete, the object returns to the frozen state and cannot be read.

<?php

// Include the autoload file to ensure that dependency libraries are correctly loaded.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the descriptions of command-line parameters.
$optsdesc = [
    "region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // Required. The region in which the bucket is located.
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // Optional. The endpoint.
    "bucket" => ['help' => 'The name of the bucket', 'required' => True], // Required. The name of the bucket.
    "key" => ['help' => 'The name of the object', 'required' => True], // Required. The name of the object.
];

// Convert the parameter descriptions to the long options format that is required by getopt.
// A colon (:) is added to the end of each parameter to indicate that the parameter requires a value.
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse the command-line parameters.
$options = getopt("", $longopts);

// Check whether required parameters exist.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help']; // Obtain the help information for the parameter.
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1); // Exit the program if a required parameter is missing.
    }
}

// Extract values from the parsed parameters.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$key = $options["key"];       // The name of the object.

// Load the credential information from environment variables.
// Use EnvironmentVariableCredentialsProvider to read the Access Key ID and Access Key Secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider.
$cfg->setRegion($region); // Set the region in which the bucket is located.
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]); // Set the endpoint if an endpoint is provided.
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Create a CleanRestoredObjectRequest object to clean up the restored Archive object.
$request = new Oss\Models\CleanRestoredObjectRequest($bucket, $key);

// Clean up the restored object.
$result = $client->cleanRestoredObject($request);

// Print the cleanup result.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, 200 indicates success.
    'request id:' . $result->requestId . PHP_EOL     // The request ID, which is used to debug or trace the request.
);

References