Updated on 2022-02-10 GMT+08:00

PUT Bucket lifecycle

API Description

You can use this API to set lifecycle rules for a bucket, so as to periodically delete objects in the bucket.

Method Definition

1. ObsClient->setBucketLifecycle(array $parameter)
2. ObsClient->setBucketLifecycleAsync(array $parameter, callable $callback)

Request Parameter

Field

Type

Optional or Mandatory

Description

Bucket

string

Mandatory

Bucket name

Rules

indexed array

Mandatory

Lifecycle rules of the bucket

  

Transitions

indexed array

Optional

List of object transition policies

  

StorageClass

string

Mandatory

Storage class of the object after transition

NOTE:

The Standard storage class is not supported.

Date

string

or

\DateTime

Mandatory when Days is not set

Date when an object will be transited. The value must conform with the ISO8601 standards and must be at 00:00 (UTC time), for example, 2018-01-01T00:00:00Z.

Days

integer

Mandatory when Date is not set

Number of days after which an object will be transited since its creation. The value must be a positive integer.

  

Expiration

associative array

Optional

Expiration time of an object

  

Date

string

or

\DateTime

Mandatory when Days is not set

Date when an object expires. If the value type is string, the value must conform to the ISO8601 standards and must be at 00:00 (UTC time), for example, 2018-01-01T00:00:00Z.

Days

integer

Mandatory when Date is not set

Number of days after which an object expires since its creation. The value must be a positive integer.

ID

string

Optional

Rule ID. It is a 1-255 character string.

Prefix

string

Mandatory

Object name prefix identifying one or more objects to which the rule applies. The value can be empty, indicating that the rule applies to all objects in the bucket.

Status

string

Mandatory

Whether this rule is enabled. Possible values are:

  • Enabled
  • Disabled
  

NoncurrentVersionTransitions

indexed array

Optional

List of noncurrent object version transition policies

  

StorageClass

string

Mandatory

Storage class of the noncurrent object version after transition

NoncurrentDays

integer

Mandatory

Number of days after which an object will be transited since it becomes a noncurrent version. The parameter value must be a positive integer.

  

NoncurrentVersionExpiration

associative array

Optional

Expiration time of a noncurrent object version

  

NoncurrentDays

integer

Mandatory

Number of days after which an object expires since it becomes a noncurrent version. The field value must be a positive integer.

Transitions, Expiration, NoncurrentVersionTransitions, and NoncurrentVersionExpiration cannot be all null.

Returned Result

Field

Type

Description

HttpStatusCode

integer

HTTP status code

Reason

string

Reason description

RequestId

string

Request ID returned by the OBS server

Sample Code

try{
       $resp = $obsClient -> setBucketLifecycle([
              'Bucket' => 'bucketname',
              'Rules' => [
                     ['ID' => 'rule1', 'Prefix' => 'prefix1', 'Status' => 'Enabled', 'Expiration' => ['Days' => 60], 'NoncurrentVersionExpiration' => ['NoncurrentDays' => 60]],
                     ['ID' => 'rule2', 'Prefix' => 'prefix2', 'Status' => 'Enabled', 'Expiration' => ['Date' => '2018-12-31T00:00:00Z']]
              ]
       ]);
       printf("RequestId:%s\n", $resp['RequestId']);
}catch (Obs\Common\ObsException $obsException){
       printf("ExceptionCode:%s\n", $obsException->getExceptionCode());
       printf("ExceptionMessage:%s\n", $obsException->getExceptionMessage());
}