All Products
Search
Document Center

Object Storage Service:Restore objects by using OSS SDK for PHP 2.0

Last Updated:Mar 20, 2026

Use the RestoreObject operation to make Archive, Cold Archive, or Deep Cold Archive objects temporarily accessible. In a versioned bucket, different versions of an object can have different storage classes. By default, RestoreObject targets the current version. To restore a specific version, include the version ID in the request.

Prerequisites

Before you begin, make sure that you have:

Usage notes

  • The sample code uses the region ID cn-hangzhou (China (Hangzhou)). By default, a public endpoint is used. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For region and endpoint details, see Regions and endpoints.

Restore an Archive, Cold Archive, or Deep Cold Archive object

<?php

// Load dependency libraries.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

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

// Build long options for getopt (each option requires a value).
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse command line arguments.
$options = getopt("", $longopts);

// Validate required arguments.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1);
    }
}

$region = $options["region"];
$bucket = $options["bucket"];
$key    = $options["key"];

// Load access credentials from environment variables.
// Use EnvironmentVariableCredentialsProvider to retrieve the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Initialize the client.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

$client = new Oss\Client($cfg);

// Submit a restore request.
// To restore a specific version, set versionId to the target version ID.
$request = new Oss\Models\RestoreObjectRequest(
    bucket:    $bucket,
    key:       $key,
    versionId: "yourVersionId", // Replace with the actual version ID.
);

$result = $client->restoreObject($request);

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

Parameters

ParameterTypeRequiredDescription
bucketStringYesThe name of the bucket.
keyStringYesThe full path of the object, excluding the bucket name.
regionStringYesThe ID of the region where the bucket is located, for example, cn-hangzhou.
endpointStringNoA custom endpoint for accessing OSS. If not specified, the public endpoint for the region is used.
versionIdStringNoThe version ID of the object to restore. If not specified, the current version is restored.

What's next