• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. API Reference (Python SDK)
  4. Bucket-Related APIs
  5. PUT Bucket lifecycle

PUT Bucket lifecycle

API Description

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

Method Definition

ObsClient.setBucketLifecycleConfiguration(bucketName, lifecycle)

Request Parameter

Field

Type

Optional or Mandatory

Description

bucketName

str

Mandatory

Bucket name

lifecycle

Lifecycle

Mandatory

Bucket lifecycle settings

Returned Result

Type

Description

GetResult

SDK common result object

Sample Code

try:
    from com.obs.models.expiration import Expiration, NoncurrentVersionExpiration 
    from com.obs.models.date_time import DateTime 
    from com.obs.models.rule import Rule 
    from com.obs.models.lifecycle import Lifecycle 
     
    rule1 = Rule(id='rule1', prefix='prefix1', status='Enabled', expiration=Expiration(days=60), noncurrentVersionExpiration=NoncurrentVersionExpiration(noncurrentDays=60)) 
    rule2 = Rule(id='rule2', prefix='prefix2', status='Enabled', expiration=Expiration(date=DateTime(2018, 12, 31))) 
    lifecycle = Lifecycle(rule=[rule1, rule2]) 
     
    resp = obsClient.setBucketLifecycleConfiguration('bucketname', lifecycle) 
     
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())