• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Python SDK)
  4. Lifecycle Management
  5. Setting Lifecycle Rules

Setting Lifecycle Rules

You can call ObsClient.setBucketLifecycleConfiguration to set lifecycle rules for a bucket.

Setting an Object Transition Policy

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

from com.obs.models.expiration import Transition, NoncurrentVersionTransition
from com.obs.models.date_time import DateTime
from com.obs.models.rule import Rule
from com.obs.models.lifecycle import Lifecycle
# Specify that objects whose names contain the prefix will be transited to OBS Warm 30 days after creation, and that objects whose names contain the prefix will be transited to OBS Cold 30 days after becoming noncurrent versions.
rule1 = Rule(id='rule1', prefix='prefix1', status='Enabled', transition=Transition(storageClass='STANDARD_IA', days=30), noncurrentVersionTransition=NoncurrentVersionTransition(storageClass='GLACIER', noncurrentDays=30))

# Directly specify the date when objects whose name contain the prefix will be transited to OBS Warm.
rule2 = Rule(id='rule2', prefix='prefix2', status='Enabled', transition=Transition(storageClass='STANDARD_IA', date=DateTime(2018, 10, 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)

Setting an Object Expiration Time

Sample code:

# Import the module.
from com.obs.client.obs_client import ObsClient
 
# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='yourdomainname'
)

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
# Specify that objects whose names contain the prefix will expire 60 days after creation, and that objects whose names contain the prefix will expire 60 days after becoming noncurrent versions.
rule1 = Rule(id='rule1', prefix='prefix1', status='Enabled', expiration=Expiration(days=60), noncurrentVersionExpiration=NoncurrentVersionExpiration(noncurrentDays=60))

# Specify a date when the objects whose names contain the prefix will expire.
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)