Manage lifecycle rules

Last Updated: Nov 02, 2017

OSS provides object lifecycle management to manage objects. You can configure the lifecycle of a bucket to define various rules for the objects in the bucket. Currently, you can define rules to delete specific objects. Each rule is composed of the following:

  • Object name prefix: The rule applies only to the objects whose names contain the specified prefix.

  • Operation: The operation to be performed on the objects that match the user-defined rule.

  • Date or number of days: The date when the specified operation are performed on objects, or the number of days following the last object modification time after which the specified operation is to be performed on objects. The Date format is not recommended for object lifecycle management, because the objects whose names are prefixed with specific content expire when the specified date is reached, regardless of the last object modification time.

For more information about the lifecycle, see Object Lifecycle.

Note: For complete code, see GitHub.

Lifecycle rules

The lifecycle configuration rules are expressed by an xml segment.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LifecycleConfiguration>
  3. <Rule>
  4. <ID>delete obsoleted files</ID>
  5. <Prefix>obsoleted/</Prefix>
  6. <Status>Enabled</Status>
  7. <Expiration><Days>3</Days></Expiration>
  8. </Rule>
  9. <Rule>
  10. <ID>delete temporary files</ID>
  11. <Prefix>temporary/</Prefix>
  12. <Status>Enabled</Status>
  13. <Expiration><Date>2022-10-12T00:00:00.000Z</Date></Expiration>
  14. </Rule>
  15. </LifecycleConfiguration>

A single lifecycle Config can contain up to 1,000 rules.

Field description:

  • The ID field uniquely identifies a rule.

  • The prefix field specifies the prefix that the names of objects in a bucket must contain to apply the rule. Duplicate prefixes are not allowed.

  • Status indicates the status of this rule. The statuses are either Enabled or Disabled, indicating if the rule is enabled or disabled.

  • In the Expiration node, Days indicates that an object gets deleted after the specified number of days after its last modification. Date indicates that an object is to be deleted after the specified absolute time (the absolute time follows the ISO8601 format).

Configure lifecycle rules

You can set the bucket lifecycle rules using OssClient::putBucketLifecycle:

  1. <?php
  2. /**
  3. * Set lifecycle rules for a bucket
  4. *
  5. * @param OssClient $ossClient OssClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function putBucketLifecycle($ossClient, $bucket)
  10. {
  11. $lifecycleConfig = new LifecycleConfig();
  12. $actions = array();
  13. $actions[] = new LifecycleAction(OssClient::OSS_LIFECYCLE_EXPIRATION, OssClient::OSS_LIFECYCLE_TIMING_DAYS, 3);
  14. $lifecycleRule = new LifecycleRule("delete obsoleted files", "obsoleted/", "Enabled", $actions);
  15. $lifecycleConfig->addRule($lifecycleRule);
  16. $actions = array();
  17. $actions[] = new LifecycleAction(OssClient::OSS_LIFECYCLE_EXPIRATION, OssClient::OSS_LIFECYCLE_TIMING_DATE, '2022-10-12T00:00:00.000Z');
  18. $lifecycleRule = new LifecycleRule("delete temporary files", "temporary/", "Enabled", $actions);
  19. $lifecycleConfig->addRule($lifecycleRule);
  20. try {
  21. $ossClient->putBucketLifecycle($bucket, $lifecycleConfig);
  22. } catch (OssException $e) {
  23. printf(__FUNCTION__ . ": FAILED\n");
  24. printf($e->getMessage() . "\n");
  25. return;
  26. }
  27. print(__FUNCTION__ . ": OK" . "\n");
  28. }

Get lifecycle rules

You can get the bucket lifecycle rules using OssClient::getBucketLifecycle:

  1. <?php
  2. /**
  3. * Get the lifecycle configuration of a bucket
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function getBucketLifecycle($ossClient, $bucket)
  10. {
  11. $lifecycleConfig = null;
  12. try{
  13. $lifecycleConfig = $ossClient->getBucketLifecycle($bucket);
  14. } catch(OssException $e) {
  15. printf(__FUNCTION__ . ": FAILED\n");
  16. printf($e->getMessage() . "\n");
  17. return;
  18. }
  19. print(__FUNCTION__ . ": OK" . "\n");
  20. print($lifecycleConfig->serializeToXml() . "\n");
  21. }

Delete lifecycle rules

You can clear the bucket lifecycle rules using OssClient::deleteBucketLifecycle:

  1. <?php
  2. /**
  3. * Delete the lifecycle configuration of a bucket
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function deleteBucketLifecycle($ossClient, $bucket)
  10. {
  11. try{
  12. $ossClient->deleteBucketLifecycle($bucket);
  13. } catch(OssException $e) {
  14. printf(__FUNCTION__ . ": FAILED\n");
  15. printf($e->getMessage() . "\n");
  16. return;
  17. }
  18. print(__FUNCTION__ . ": OK" . "\n");
  19. }
Thank you! We've received your feedback.