Help Center/ Object Storage Service/ SDK Reference/ Python/ Bucket-Related APIs (SDK for Python)/ Obtaining the Lifecycle Configuration of a Bucket (SDK for Python)
Updated on 2024-09-05 GMT+08:00

Obtaining the Lifecycle Configuration of a Bucket (SDK for Python)

Function

You can configure lifecycle rules to periodically delete objects or transition objects between storage classes. For more information, see Lifecycle Management.

This API returns the lifecycle rules of a bucket.

Restrictions

Method

ObsClient.getBucketLifecycle(bucketName)

Request Parameters

Parameter

Type

Mandatory (Yes/No)

Description

bucketName

str

Yes

Explanation:

Bucket name

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets of the same name in the same region, no error will be reported and the bucket properties comply with those set in the first creation request.

Default value:

None

Responses

Type

Description

GetResult

Explanation:

SDK common results

Table 1 GetResult

Parameter

Type

Description

status

int

Explanation:

HTTP status code

Value range:

A status code is a group of digits ranging from 2xx (indicating successes) to 4xx or 5xx (indicating errors). It indicates the status of a response. For more information, see Status Code.

Default value:

None

reason

str

Explanation:

Reason description.

Default value:

None

errorCode

str

Explanation:

Error code returned by the OBS server. If the value of status is less than 300, this parameter is left blank.

Default value:

None

errorMessage

str

Explanation:

Error message returned by the OBS server. If the value of status is less than 300, this parameter is left blank.

Default value:

None

requestId

str

Explanation:

Request ID returned by the OBS server

Default value:

None

indicator

str

Explanation:

Error indicator returned by the OBS server.

Default value:

None

hostId

str

Explanation:

Requested server ID. If the value of status is less than 300, this parameter is left blank.

Default value:

None

resource

str

Explanation:

Error source (a bucket or an object). If the value of status is less than 300, this parameter is left blank.

Default value:

None

header

list

Explanation:

Response header list, composed of tuples. Each tuple consists of two elements, respectively corresponding to the key and value of a response header.

Default value:

None

body

object

Explanation:

Result content returned after the operation is successful. If the value of status is larger than 300, this parameter is left blank. The value varies with the API being called. For details, see Bucket-Related APIs (SDK for Python) and Object-Related APIs (SDK for Python).

Default value:

None

GetResult.body Type

Description

LifecycleResponse

Explanation:

Response to the request for obtaining the lifecycle configuration information of a bucket

Default value:

None

Table 2 LifecycleResponse

Parameter

Type

Description

lifecycleConfig

Lifecycle

Explanation:

Bucket lifecycle configuration. For details, see Table 3.

Default value:

None

Table 3 Lifecycle

Parameter

Type

Mandatory (Yes/No)

Description

rule

list of Rule

Yes if used as a request parameter

Explanation:

List of lifecycle rules. For details, see Table 4.

Default value:

None

Table 4 Rule

Parameter

Type

Mandatory (Yes/No)

Description

id

str

No if used as a request parameter

Explanation:

Lifecycle rule ID

Value range:

The value must contain 1 to 255 characters.

Default value:

None

prefix

str

Yes if used as a request parameter

Explanation:

Object name prefix. It identifies the objects the rule applies to. You can leave this parameter blank to apply the rule to all objects in the bucket.

Assume that you have the following objects: logs/day1, logs/day2, logs/day3, and ExampleObject.jpg. If you set Prefix to ExampleObject.jpg, the rule applies to object ExampleObject.jpg only. If you set Prefix to logs/, the rule applies to the three objects with name starting with logs/. If you leave Prefix blank, the rule applies to all objects in the bucket.

Value range:

The value must contain 1 to 1,024 characters.

Default value:

None

status

str

Yes if used as a request parameter

Explanation:

Whether the rule is enabled

Value range:

  • Enabled
  • Disabled

Default value:

None

transition

Transition

or

list of Transition

No if used as a request parameter

Explanation:

Policies for storage class transition, including transition time and the storage class after transition. For details, see Table 5.

Restrictions:

This parameter applies only to the current object version.

Default value:

None

expiration

Expiration

No if used as a request parameter

Explanation:

Object expiration time. For details, see Table 7.

Restrictions:

This parameter applies only to the current object version.

Default value:

None

noncurrentVersionTransition

NoncurrentVersionTransition

or

list of NoncurrentVersionTransition

No if used as a request parameter

Explanation:

Policies for storage class transition, including transition time and the storage class after transition. For details, see Table 9.

Restrictions:

  • This parameter applies only to noncurrent object versions.
  • Versioning is enabled (or suspended after being enabled) for the bucket.
  • This parameter is not available for parallel file systems.

Default value:

None

noncurrentVersionExpiration

NoncurrentVersionExpiration

No if used as a request parameter

Explanation:

Expiration time of noncurrent object versions. For details, see Table 10.

Restrictions:

  • This parameter applies only to noncurrent object versions.
  • Versioning is enabled (or suspended after being enabled) for the bucket.
  • This parameter is not available for parallel file systems.

Default value:

None

Table 5 Transition

Parameter

Type

Mandatory (Yes/No)

Description

storageClass

str

Yes if used as a request parameter

Explanation:

Storage class of the object after transition

Restrictions:

  • The Standard storage class is not supported.
  • Restrictions on storage class transitions:
    • Only transitions from the Standard storage class to the Infrequent Access storage class are supported. To transition objects from Infrequent Access to Standard, you must manually do it.
    • Only transitions from the Standard or Infrequent Access storage class to the Archive storage class are supported. To transition objects from Archive to Standard or Infrequent Access, you must restore the objects first and then manually transition their storage class.
    • Multi-AZ redundancy is not available for Archive storage. For this reason, buckets or objects with multi-AZ redundancy cannot be transitioned to the Archive storage class based on a lifecycle rule.

Value range:

See Table 6.

Default value:

None

date

str

or

DateTime

Yes if the parameter is used as a request parameter and days is absent

Explanation:

Date when an object will be transitioned.

Restrictions:

The value must conform with the ISO8601 standards and must be at 00:00 (UTC time). For example, 2018-01-01T00:00:00.000Z, indicating only objects that were last modified before that time are transitioned to the specified storage class.

For example, DateTime(year=2023, month=9, day=12)

Default value:

None

days

int

Yes if the parameter is used as a request parameter and date is absent

Explanation:

Number of days (since the last update was made to the object) after which the lifecycle rule takes effect

Restrictions:

This parameter applies only to the current object version.

Value range:

An integer greater than or equal to 0, in days

Default value:

None

Table 6 StorageClass

Parameter

Type

Description

STANDARD

Standard storage class

Explanation:

Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response.

WARM

Infrequent Access storage class

Explanation:

Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but is instantly available when needed.

COLD

Archive storage class

Explanation:

Used for storing rarely accessed (once a year) data.

Table 7 Expiration

Parameter

Type

Mandatory (Yes/No)

Description

date

str

or

DateTime

Yes if the parameter is used as a request parameter and days is absent

Explanation:

OBS executes the lifecycle rule for objects that were modified before the specified date.

Restrictions:

The value must conform with the ISO8601 standards and must be at 00:00 (UTC time). For example, 2018-01-01T00:00:00.000Z, indicating only objects that were last modified before that time are deleted.

For example, DateTime(year=2023, month=9, day=12)

Default value:

None

days

int

Yes if the parameter is used as a request parameter and date is absent

Explanation:

Number of days (since the last update was made to the object) after which the lifecycle rule takes effect

Restrictions:

This parameter applies only to the current object version.

Value range:

An integer greater than or equal to 0, in days

Default value:

None

Table 8 DateTime

Parameter

Type

Description

year

int

Explanation:

Year in UTC

Default value:

None

month

int

Explanation:

Month in UTC

Default value:

None

day

int

Explanation:

Day in UTC

Default value:

None

hour

int

Explanation:

Hour in UTC

Restrictions:

The value is in 24-hour format.

Default value:

0

min

int

Explanation:

Minute in UTC

Default value:

0

sec

int

Explanation:

Second in UTC

Default value:

0

Table 9 NoncurrentVersionTransition

Parameter

Type

Mandatory (Yes/No)

Description

storageClass

str

Yes if used as a request parameter

Explanation:

Storage class of noncurrent object versions after transition

Restrictions:

  • The Standard storage class is not supported.
  • Restrictions on storage class transitions:
    • Only transitions from the Standard storage class to the Infrequent Access storage class are supported. To transition objects from Infrequent Access to Standard, you must manually do it.
    • Only transitions from the Standard or Infrequent Access storage class to the Archive storage class are supported. To transition objects from Archive to Standard or Infrequent Access, you must restore the objects first and then manually transition their storage class.
    • Multi-AZ redundancy is not available for Archive storage. For this reason, buckets or objects with multi-AZ redundancy cannot be transitioned to the Archive storage class based on a lifecycle rule.

Value range:

See Table 6.

Default value:

None

noncurrentDays

int

Yes if used as a request parameter

Explanation:

Number of days an object is noncurrent before the specified rule takes effect

Restrictions:

This parameter applies only to noncurrent object versions.

Value range:

An integer greater than or equal to 0, in days

Default value:

None

Table 10 NoncurrentVersionExpiration

Parameter

Type

Mandatory (Yes/No)

Description

noncurrentDays

int

Yes if used as a request parameter

Explanation:

Number of days an object is noncurrent before the specified rule takes effect

Restrictions:

This parameter applies only to noncurrent object versions.

Value range:

An integer greater than or equal to 0, in days

Default value:

None

Code Examples

This example returns the lifecycle configuration of bucket examplebucket.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from obs import ObsClient
import os
import traceback

# Obtain an AK and SK pair using environment variables or import the AK and SK pair in other ways. Using hard coding may result in leakage.
# Obtain an AK and SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html.
ak = os.getenv("AccessKeyID")
sk = os.getenv("SecretAccessKey")
# (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them from environment variables.
security_token = os.getenv("SecurityToken")
# Set server to the endpoint corresponding to the bucket. Here uses EU-Dublin as an example. Replace it with the one in use.
server = "https://obs.eu-west-101.myhuaweicloud.eu" 

# Create an obsClient instance.
# If you use a temporary AK and SK pair and a security token to access OBS, you must specify security_token when creating an instance.
obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
try:
    bucketName="examplebucket"
    # Obtain the lifecycle configuration of the bucket.
    resp = obsClient.getBucketLifecycle(bucketName)
    # If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
    if resp.status < 300:
        print('Get Bucket Lifecycle Succeeded')
        print('requestId:', resp.requestId)
        index = 1
        for rule in resp.body.lifecycleConfig.rule:
            print('rule [' + str(index) + ']')
            print('id:', rule.id)
            print('prefix:', rule.prefix)
            print('status:', rule.status)
            if rule.expiration:
                print('days:', rule.expiration.days)
                print('date:', rule.expiration.date)
            if rule.noncurrentVersionExpiration:
                print('noncurrentDays:', rule.noncurrentVersionExpiration.noncurrentDays)
        index += 1
    else:
        print('Get Bucket Lifecycle Failed')
        print('requestId:', resp.requestId)
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except:
    print('Get Bucket Lifecycle Failed')
    print(traceback.format_exc())