Tous les produits
Search
Centre de documentation

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

Dernière mise à jour :Aug 18, 2026

Vous devez restaurer les objets de classe de stockage Archive, Cold Archive et Deep Cold Archive avant de pouvoir les lire. La restauration d'un objet génère un réplica temporaire qui coexiste avec l'objet d'origine. Le réplica est automatiquement supprimé à l'expiration de la période de restauration.

Notes

  • L'opération RestoreObject s'applique uniquement aux objets de classe de stockage Archive, Cold Archive et Deep Cold Archive.

  • L'exemple de code de cette rubrique utilise l'ID de région cn-hangzhou de la région Chine (Hangzhou) et accède aux ressources via un endpoint public par défaut. Pour accéder aux ressources depuis d'autres services Alibaba Cloud dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et endpoints pris en charge, consultez Régions et endpoints.

  • La restauration d'objets nécessite l'autorisation oss:RestoreObject. Pour plus d'informations, consultez Accorder une politique personnalisée.

Exemple de code

L'exemple de code suivant montre comment restaurer des objets de classe de stockage Archive, Cold Archive ou Deep Cold Archive.

<?php

// Include the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

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

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

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

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

// Assign the values parsed from the command-line parameters to the corresponding variables.
$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 access credentials from environment variables.
// Use EnvironmentVariableCredentialsProvider to retrieve the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

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

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

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

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

// Display 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     // The request ID, which can be used to debug or trace the request.
);

Scénarios courants

Supprimer un réplica Standard à l'aide de la méthode CleanRestoredObject

La restauration d'un objet Cold Archive ou Deep Cold Archive dans OSS génère un réplica Standard pour un accès temporaire. Vous êtes facturé pour le stockage temporaire de ce réplica jusqu'à ce que l'objet retourne à l'état gelé. Pour supprimer prématurément l'objet de l'état restauré et cesser d'encourir des coûts de stockage pour le réplica, envoyez une requête CleanRestoredObject. Une fois la requête terminée, l'objet retourne à l'état gelé et ne peut plus être lu directement.

<?php

// Include the autoload file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

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

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

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

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

// Assign the values parsed from the command-line parameters to the corresponding variables.
$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 access credentials from environment variables.
// Use EnvironmentVariableCredentialsProvider to retrieve the AccessKey ID and AccessKey secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

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

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

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

// Execute the request.
$result = $client->cleanRestoredObject($request);

// Display the result of the request.
printf(
    'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, HTTP status code 200 indicates that the request is successful.
    'request id:' . $result->requestId . PHP_EOL     // The request ID, which can be used to debug or trace the request.
);

Références

  • Pour obtenir l'exemple de code complet de restauration d'un objet, visitez GitHub.

  • Pour plus d'informations sur l'opération API RestoreObject, consultez RestoreObject.

  • Pour plus d'informations sur la restauration d'objets, consultez Restaurer des objets.