Object Storage Service (OSS) lets you set time-based retention policies for buckets. The retention period can range from 1 day to 70 years. This topic describes how to create, retrieve, lock, and cancel a retention policy.
Usage notes
-
The sample code in this topic uses the China (Hangzhou) region ID
cn-hangzhouand a public endpoint by default. To access OSS from other Alibaba Cloud products in the same region, you can use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints. -
This topic provides an example of how to read access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
Sample code
Create a retention policy
Use the following code to create a retention policy:
<?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.
);
Cancel an unlocked retention policy
Use the following code to cancel an unlocked retention policy:
<?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.
);
Get a retention policy
Use the following code to retrieve the configuration of a retention policy:
<?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.
);
Extend the retention period of an Object
The following code extends the retention period of a locked retention policy:
<?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.
);
Related documents
-
For more information about how to create a retention policy, see InitiateBucketWorm.
-
For more information about how to cancel an unlocked retention policy, see AbortBucketWorm.
-
For more information about how to retrieve the configuration of a retention policy, see GetBucketWorm.
-
For more information about how to extend the retention period of a retention policy, see ExtendBucketWorm.