Updated on 2026-09-22 GMT+08:00

Setting Lifecycle Rules

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.

You can call ObsClient->setBucketLifecycle to set lifecycle rules for a bucket.

Setting an Object Transition Policy

Sample code:
<?php

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';

// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([ 
    // Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    'key' => getenv('ACCESS_KEY_ID'),
    'secret' => getenv('SECRET_ACCESS_KEY'),
    'endpoint' => 'https://your-endpoint',

]);

try {
    $resp = $obsClient->setBucketLifecycle([ 
        'Bucket' => 'bucketname',
        'Rules' => [ 
            [ 
                'ID' => 'rule1',
                'Prefix' => 'prefix1',
                'Status' => 'Enabled',
                // Specify that objects whose names contain the specified prefix will be transitioned to Infrequent Access 30 days after creation.
                'Transitions' => [ 
                    [
                        'StorageClass' => ObsClient::StorageClassWarm,
                        'Days' => 30
                    ] 
                ],
                // Specify that objects whose names contain the specified prefix will be transitioned to Archive after being noncurrent versions for 30 days.
                'NoncurrentVersionTransitions' => [ 
                    [
                        'StorageClass' => ObsClient::StorageClassCold,
                        'NoncurrentDays' => 30
                    ]                                        
                ] 
            ],
            [ 
                'ID' => 'rule2',
                'Prefix' => 'prefix2',
                'Status' => 'Enabled',
                // Specify the date when objects whose names contain the specified prefix will be transitioned to Infrequent Access.
                'Transitions' => [ 
                    [
                        'StorageClass' => ObsClient::StorageClassWarm,
                        'Date' => '2018-12-31T00:00:00Z'
                    ]                                       
                ] 
            ] 
        ] 
    ]);
    printf( "RequestId:%s\n", $resp['RequestId']);
} catch (ObsException $e) {
      // If the operation fails, obtain the exception details.
      printf("SetBucketLifecycleFailed: Status:%d, Code:%s, Message:%s\n",
         $e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
      // Close ObsClient to release resources.
      $obsClient->close();
}

Setting an Object Expiration Time

Sample code:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
// Create an ObsClient instance.
$obsClient = new ObsClient([ 
    // Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.    'key' => getenv('ACCESS_KEY_ID'),
    'secret' => getenv('SECRET_ACCESS_KEY'),
    'endpoint' => 'https://your-endpoint',
]);

try {
    // Configure a lifecycle rule for a bucket.
    $resp = $obsClient->setBucketLifecycle([ 
        'Bucket' => 'bucketname',
        'Rules' => [
             [
                 'ID' => 'rule1',
                 'Prefix' => 'prefix1',
                 'Status' => 'Enabled',
                 // Specify that objects whose names contain the specified prefix will expire 60 days after creation.
                 'Expiration' => [ 
                     'Days' => 60
                 ],
                 // Specify that objects whose names contain the specified prefix will expire after changing into noncurrent versions for 60 days.
                 'NoncurrentVersionExpiration' => [ 
                     'NoncurrentDays' => 60 
                 ] 
             ],
             [ 
                 'ID' => 'rule2',
                 'Prefix' => 'prefix2',
                 'Status' => 'Enabled',
                 // Specify when the objects whose names contain the specified prefix will expire. The value must conform to the ISO8601 standard and must be at 00:00 (UTC time).
                 'Expiration' => [ 
                         'Date' => '2018-12-31T00:00:00Z' 
                  ] 
             ] 
       ] 
    ]);

    printf("RequestId:%s\n",$resp['RequestId']);
} catch (ObsException $e) {
      // If the operation fails, obtain the exception details.
      printf("SetBucketLifecycleFailed: Status:%d, Code:%s, Message:%s\n",
         $e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
      // Close ObsClient to release resources.
      $obsClient->close();
}

Use the Rules parameter to specify the lifecycle rules for a bucket.