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
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
Bucket |
string |
Yes |
Explanation: Bucket name Restrictions:
Value range: The value can contain 3 to 63 characters. Default value: None |
Rules |
Yes |
Explanation: Lifecycle rules of the bucket Restrictions:
Value range: See Table 2. Default value: None |
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 |
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 |
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 |
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:
Value range: See Table 5. Default value: None |
|
NoncurrentVersionExpiration |
No if used as a request parameter |
Explanation: Expiration time of historical object versions Restrictions:
Value range: See Table 6. Default value: None |
Transitions, Expiration, NoncurrentVersionTransitions, and NoncurrentVersionExpiration cannot be left blank at the same time.
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
StorageClass |
Yes if used as a request parameter |
Explanation: Storage class of the object after transition Restrictions:
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 |
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 |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
StorageClass |
Yes if used as a request parameter |
Explanation: Storage class of historical object versions after transition Restrictions:
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 |
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 |
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
Type |
Description |
---|---|
NOTE:
This API returns a Promise response, which requires the Promise or async/await syntax. |
Explanation: Returned results. For details, see Table 9. |
Parameter |
Type |
Description |
---|---|---|
CommonMsg |
Explanation: Common information generated after an API call is complete, including the HTTP status code and error code. For details, see Table 10. |
|
InterfaceResult |
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. |
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. |
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(); |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot