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
- To obtain the lifecycle configuration of a bucket, you must be the bucket owner or have the required permission (obs:bucket:GetLifecycleConfiguration in IAM or GetLifecycleConfiguration in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Creating a Custom Bucket Policy.
Method
ObsClient.getBucketLifecycle(bucketName)
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
bucketName |
str |
Yes |
Explanation: Bucket name Restrictions:
Default value: None |
Responses
Type |
Description |
---|---|
Explanation: SDK common results |
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 |
---|---|
Explanation: Response to the request for obtaining the lifecycle configuration information of a bucket Default value: None |
Parameter |
Type |
Description |
---|---|---|
lifecycleConfig |
Explanation: Bucket lifecycle configuration. For details, see Table 3. Default value: None |
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 |
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:
Default value: None |
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 |
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 |
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:
Default value: None |
noncurrentVersionExpiration |
No if used as a request parameter |
Explanation: Expiration time of noncurrent object versions. For details, see Table 10. Restrictions:
Default value: None |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
storageClass |
str |
Yes if used as a request parameter |
Explanation: Storage class of the object after transition Restrictions:
Value range: See Table 6. Default value: None |
date |
str or |
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 |
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. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
date |
str or |
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 |
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 |
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:
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 |
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()) |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.