Help Center/ Object Storage Service/ SDK Reference/ Node.js/ Lifecycle (SDK for Node.js)/ Configuring Lifecycle Rules for a Bucket (SDK for Node.js)
Updated on 2024-11-13 GMT+08:00

Configuring Lifecycle Rules for a Bucket (SDK for Node.js)

If you have any questions during development, post them on the Issues page of GitHub.

Function

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

This API configures lifecycle rules for a bucket.

  • Expired objects will be permanently deleted and cannot be restored.
  • 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.
  • The minimum storage duration is 30 days for Infrequent Access storage and 90 days for Archive storage. After an object is transitioned to the Archive storage class, if it stays in this storage class for less than 90 days, you still need to pay for a full 90 days.

Restrictions

  • There is no limit on the number of lifecycle rules in a bucket, but the total size of XML descriptions of all lifecycle rules in a bucket cannot exceed 20 KB.
  • A maximum of 20 lifecycle rules can be configured for a parallel file system.
  • To configure a lifecycle rule for a bucket, you must be the bucket owner or have the required permission (obs:bucket:PutLifecycleConfiguration in IAM or PutLifecycleConfiguration in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Creating a Custom Bucket Policy.
  • To learn about the mappings between OBS regions and endpoints, see Regions and Endpoints.

Method

ObsClient.setBucketLifecycle(params)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

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 a period (.) and a hyphen (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets with the same name in the same region, no error will be reported, and the bucket attributes comply with those set in the first creation request.

Value range:

The value can contain 3 to 63 characters.

Default value:

None

Rules

LifecycleRule[]

Yes

Explanation:

Lifecycle rules of the bucket

Restrictions:

  • There is no limit on the number of lifecycle rules in a bucket, but the total size of XML descriptions of all lifecycle rules in a bucket cannot exceed 20 KB.
  • A maximum of 20 lifecycle rules can be configured for a parallel file system.

Value range:

See Table 2.

Default value:

None

Table 2 LifecycleRule

Parameter

Type

Mandatory (Yes/No)

Description

ID

string

No if used as a request parameter

Explanation:

Lifecycle rule ID.

Restrictions:

None

Value range:

The value must contain 1 to 255 characters.

Default value:

None

Prefix

string

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 alone. If you set Prefix to logs/, the rule applies to objects logs/day1, logs/day2, and logs/day3. If you leave Prefix blank, the rule applies to all objects in the bucket.

Restrictions:

None

Value range:

The value can contain 1 to 1,024 characters.

Default value:

None

Status

string

Yes if used as a request parameter

Explanation:

Whether the current rule is enabled.

Restrictions:

None

Value range:

Enabled

Disabled

Default value:

None

Transitions

Transition[]

No if used as a request parameter

Explanation:

Policies for storage class transition, including transition time and the storage class after transition.

Restrictions:

This parameter is only available for the latest object version.

Value range:

See Table 3.

Default value:

None

Expiration

Expiration

No if used as a request parameter

Explanation:

Expiration time of an object.

Restrictions:

This parameter is only available for the latest object version.

Value range:

See Table 4.

Default value:

None

NoncurrentVersionTransitions

NoncurrentVersionTransition[]

No if used as a request parameter

Explanation:

Policies for storage class transition of historical versions, including transition time and the storage class after transition.

Restrictions:

  • This parameter is only available for historical object versions.
  • Versioning must be enabled (or suspended after being enabled) for the bucket.

Value range:

See Table 5.

Default value:

None

NoncurrentVersionExpiration

NoncurrentVersionExpiration

No if used as a request parameter

Explanation:

Expiration time of historical object versions

Restrictions:

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

Value range:

See Table 6.

Default value:

None

Transitions, Expiration, NoncurrentVersionTransitions, and NoncurrentVersionExpiration cannot be left blank at the same time.

Table 3 Transition

Parameter

Type

Mandatory (Yes/No)

Description

StorageClass

StorageClassType

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 first restore these objects and then manually transition their storage classes.
    • Multi-AZ redundancy is not available for the 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 7.

Default value:

None

Date

string

Yes if this 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 indicate UTC 00:00. For example, 2018-01-01T00:00:00.000Z indicates only objects that were last modified before the specified time are transitioned to the specified storage class.

Value range:

None

Default value:

None

Days

number

Yes if this parameter is used as a request parameter and Date is absent

Explanation:

How many days can pass since the last update before the lifecycle rule takes effect

Restrictions:

This parameter is only available for the latest object version.

Value range:

0 to (231 – 1), in days

Default value:

None

Table 4 Expiration

Parameter

Type

Mandatory (Yes/No)

Description

Date

string

Yes if this parameter is used as a request parameter and Days is absent

Explanation:

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

Restrictions:

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

Value range:

None

Default value:

None

Days

number

Yes if this parameter is used as a request parameter and Date is absent

Explanation:

How many days can pass since the last update before the lifecycle rule (deletion) takes effect

Restrictions:

This parameter is only available for the latest object version.

Value range:

1 to (231 – 1), in days

Default value:

None

Table 5 NoncurrentVersionTransition

Parameter

Type

Mandatory (Yes/No)

Description

StorageClass

StorageClassType

Yes if used as a request parameter

Explanation:

Storage class of historical 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 first restore these objects and then manually transition their storage classes.

    • 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 7.

Default value:

None

NoncurrentDays

number

Yes if used as a request parameter

Explanation:

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

Restrictions:

This parameter is only available for historical object versions.

Value range:

0 to (231 – 1), in days

Default value:

None

Table 6 NoncurrentVersionExpiration

Parameter

Type

Mandatory (Yes/No)

Description

NoncurrentDays

number

Yes if used as a request parameter

Explanation:

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

Restrictions:

This parameter is only available for historical object versions.

Value range:

0 to (231 – 1), in days

Default value:

None

Table 7 StorageClassType

Constant

Default Value

Description

ObsClient.enums.StorageClassStandard

STANDARD

Standard storage class.

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.

ObsClient.enums.StorageClassWarm

WARM

Infrequent Access storage class.

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

ObsClient.enums.StorageClassCold

COLD

Archive storage class.

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

Responses

Table 8 Responses

Type

Description

Table 9

NOTE:

This API returns a Promise response, which requires the Promise or async/await syntax.

Explanation:

Returned results. For details, see Table 9.

Table 9 Response

Parameter

Type

Description

CommonMsg

ICommonMsg

Explanation:

Common information generated after an API call is complete, including the HTTP status code and error code. For details, see Table 10.

InterfaceResult

Table 11

Explanation:

Results outputted for a successful call. For details, see Table 11.

Restrictions:

This parameter is not included if the value of Status is greater than 300.

Table 10 ICommonMsg

Parameter

Type

Description

Status

number

Explanation:

HTTP status code returned by the OBS server.

Value range:

A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For details, see Status Codes.

Code

string

Explanation:

Error code returned by the OBS server.

Message

string

Explanation:

Error description returned by the OBS server.

HostId

string

Explanation:

Request server ID returned by the OBS server.

RequestId

string

Explanation:

Request ID returned by the OBS server.

Id2

string

Explanation:

Request ID2 returned by the OBS server.

Indicator

string

Explanation:

Error code details returned by the OBS server.

Table 11 BaseResponseOutput

Parameter

Type

Description

RequestId

string

Explanation:

Request ID returned by the OBS server

Code Examples: Setting an Object Transition Policy

This example sets lifecycle rules to specify transition policies for latest and historical versions of objects in bucket examplebucket.

Code Examples: Setting an Object Expiration Time

This example sets lifecycle rules to specify expiration time for latest and historical versions of objects in 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Import the OBS library.
// Use npm to install the client.
const ObsClient = require("esdk-obs-nodejs");
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
const obsClient = new ObsClient({
  // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage.
  // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
  access_key_id: process.env.ACCESS_KEY_ID,
  secret_access_key: process.env.SECRET_ACCESS_KEY,
  // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
  // security_token: process.env.SECURITY_TOKEN,
  // Enter the endpoint corresponding to the region where the bucket is located. CN-Hong Kong is used here in this example. Replace it with the one currently in use.
  server: "https://obs.ap-southeast-1.myhuaweicloud.com"
});

async function setBucketLifecycle() {
  try {
    const params = {
      // Specify the bucket name.
      Bucket: "examplebucket",
      // Lifecycle rules
      Rules: [
        {
            ID:'rule1',Prefix:'prefix1',Status:'Enabled',
            // Specify that objects whose names contain the specified prefix will expire 60 days after creation.
            Expiration:{Days:60},
            // Specify that objects whose names contain the prefix will expire 60 days after becoming historical.
            NoncurrentVersionExpiration:{NoncurrentDays : 60}
        },
        {
            ID:'rule2',Prefix:'prefix2',Status:'Enabled',
            // Specify when the objects whose names contain the specified prefix will expire. The value must conform to the ISO8601 standard. The time value must be 00:00:00 in UTC.
            Expiration:{Date: '2018-12-31T00:00:00Z'},
        }

      ]
    };
    // Configure lifecycles.
    const result = await obsClient.setBucketLifecycle(params);
    if (result.CommonMsg.Status <= 300) {
      console.log("Set bucket(%s)'s lifecycle configuraion successful!", params.Bucket);
      console.log("RequestId: %s", result.CommonMsg.RequestId);
      return;
    };
    console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response.");
    console.log("Status: %d", result.CommonMsg.Status);
    console.log("Code: %s", result.CommonMsg.Code);
    console.log("Message: %s", result.CommonMsg.Message);
    console.log("RequestId: %s", result.CommonMsg.RequestId);
  } catch (error) {
    console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.");
    console.log(error);
  };
};

setBucketLifecycle();