Object Storage Service (OSS) vous permet de définir des politiques de rétention temporelles pour les buckets. La période de rétention peut varier de 1 jour à 70 ans. Cette rubrique explique comment créer, récupérer, verrouiller et annuler une politique de rétention.
Remarques d'utilisation
Le code exemple de cette rubrique utilise par défaut l'ID de région Chine (Hangzhou)
cn-hangzhouet un endpoint public. Pour accéder à OSS depuis d'autres produits Alibaba Cloud dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.Cette rubrique fournit un exemple montrant comment lire les identifiants d'accès à partir des variables d'environnement. Pour plus d'informations sur la configuration des identifiants d'accès, consultez Configurer les identifiants d'accès.
Exemple de code
Créer une politique de rétention
Utilisez le code suivant pour créer une politique de rétention :
<?php
// Import the autoloader file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define command line argument descriptions.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located', 'required' => True], // Required. The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS', 'required' => False], // Optional. The domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // Required. The name of the bucket.
];
// Generate a long options list to parse command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon after each argument to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command line arguments.
$options = getopt("", $longopts);
// Check if any required arguments are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing.
exit(1);
}
}
// Get the command line argument values.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
// Load credential information, such as AccessKey ID and AccessKey secret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the software development kit (SDK).
$cfg = Oss\Config::loadDefault();
// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a request object to initialize the retention policy for the bucket. Set the retention period to 3 days.
$request = new Oss\Models\InitiateBucketWormRequest(
bucket: $bucket,
initiateWormConfiguration: new Oss\Models\InitiateWormConfiguration(
retentionPeriodInDays: 3 // The retention period in days.
));
// Call the initiateBucketWorm method to initialize the retention policy for the bucket.
$result = $client->initiateBucketWorm($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'worm id:' . $result->wormId // The ID of the retention policy.
);
Annuler une politique de rétention non verrouillée
Utilisez le code suivant pour annuler une politique de rétention non verrouillée :
<?php
// Import the autoloader file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define command line argument descriptions.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located', 'required' => True], // Required. The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS', 'required' => False], // Optional. The domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // Required. The name of the bucket.
];
// Generate a long options list to parse command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon after each argument to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command line arguments.
$options = getopt("", $longopts);
// Check if any required arguments are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing.
exit(1);
}
}
// Get the command line argument values.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
// Load credential information, such as AccessKey ID and AccessKey secret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a request object to cancel the retention policy of the bucket.
$request = new Oss\Models\AbortBucketWormRequest(bucket: $bucket);
// Call the abortBucketWorm method to cancel the retention policy of the bucket.
$result = $client->abortBucketWorm($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId // The unique ID of the request.
);
Récupérer une politique de rétention
Utilisez le code suivant pour récupérer la configuration d'une politique de rétention :
<?php
// Import the autoloader file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define command line argument descriptions.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located', 'required' => True], // Required. The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS', 'required' => False], // Optional. The domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // Required. The name of the bucket.
"worm-id" => ['help' => 'The worm id of the bucket', 'required' => True], // Required. The ID of the retention policy.
];
// Generate a long options list to parse command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon after each argument to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command line arguments.
$options = getopt("", $longopts);
// Check if any required arguments are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing.
exit(1);
}
}
// Get the command line argument values.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$wormId = $options["worm-id"]; // The ID of the retention policy.
// Load credential information, such as AccessKey ID and AccessKey secret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a request object to get the retention policy of the bucket.
$request = new Oss\Models\GetBucketWormRequest(bucket: $bucket);
// Call the getBucketWorm method to get the retention policy of the bucket.
$result = $client->getBucketWorm($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId . PHP_EOL . // The unique ID of the request.
'worm config:' . var_export($result->wormConfiguration, true) // The content of the retention policy configuration.
);
Prolonger la période de rétention d'un objet
Le code suivant prolonge la période de rétention d'une politique de rétention verrouillée :
<?php
// Import the autoloader file to load dependencies.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define command line argument descriptions.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located', 'required' => True], // Required. The region where the bucket is located.
"endpoint" => ['help' => 'The domain names that other services can use to access OSS', 'required' => False], // Optional. The domain name that other services can use to access OSS.
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // Required. The name of the bucket.
"worm-id" => ['help' => 'The worm id of the bucket', 'required' => True], // Required. The ID of the retention policy.
];
// Generate a long options list to parse command line arguments.
$longopts = \array_map(function ($key) {
return "$key:"; // Add a colon after each argument to indicate that a value is required.
}, array_keys($optsdesc));
// Parse the command line arguments.
$options = getopt("", $longopts);
// Check if any required arguments are missing.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help'];
echo "Error: the following arguments are required: --$key, $help"; // Prompt the user that a required argument is missing.
exit(1);
}
}
// Get the command line argument values.
$region = $options["region"]; // The region where the bucket is located.
$bucket = $options["bucket"]; // The name of the bucket.
$wormId = $options["worm-id"]; // The ID of the retention policy.
// Load credential information, such as AccessKey ID and AccessKey secret, from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
// Set the credential provider.
$cfg->setCredentialsProvider($credentialsProvider);
// Set the region.
$cfg->setRegion($region);
// If an endpoint is provided, set the endpoint.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]);
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a request object to extend the retention policy of the bucket. The extension period is 3 days.
$request = new Oss\Models\ExtendBucketWormRequest(
bucket: $bucket,
wormId: $wormId,
extendWormConfiguration: new Oss\Models\ExtendWormConfiguration(
retentionPeriodInDays: 3 // The extension period is 3 days.
));
// Call the extendBucketWorm method to extend the retention policy of the bucket.
$result = $client->extendBucketWorm($request);
// Print the returned result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP response status code.
'request id:' . $result->requestId // The unique ID of the request.
);
Documentation connexe
Pour plus d'informations sur la création d'une politique de rétention, consultez InitiateBucketWorm.
Pour plus d'informations sur l'annulation d'une politique de rétention non verrouillée, consultez AbortBucketWorm.
Pour plus d'informations sur la récupération de la configuration d'une politique de rétention, consultez GetBucketWorm.
Pour plus d'informations sur la prolongation de la période de rétention d'une politique de rétention, consultez ExtendBucketWorm.