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:
An OSS bucket containing Archive, Cold Archive, or Deep Cold Archive objects
The
oss:RestoreObjectpermission. For details, see Attach a custom policy to a RAM user
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
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | String | Yes | The name of the bucket. |
key | String | Yes | The full path of the object, excluding the bucket name. |
region | String | Yes | The ID of the region where the bucket is located, for example, cn-hangzhou. |
endpoint | String | No | A custom endpoint for accessing OSS. If not specified, the public endpoint for the region is used. |
versionId | String | No | The version ID of the object to restore. If not specified, the current version is restored. |
What's next
For the complete sample code, see RestoreObject.php on GitHub.
For the RestoreObject API reference, see RestoreObject.
To learn about restore behavior and restore states, see Restore objects.