Configure lifecycle rules

Updated at:

You can configure lifecycle rules based on the last modified time of objects to regularly delete objects and parts from an Object Storage Service (OSS) on CloudBox bucket to reduce storage costs.

Prerequisites

  • OSS on CloudBox is available only in the China (Hangzhou), China (Shanghai), China (Shenzhen), China (Heyuan), China (Beijing), and China (Chengdu) regions.

  • You have purchased a CloudBox.

  • You have created a VPC and a vSwitch for the CloudBox.

  • You have contacted technical support to request a SingleTunnel network type for your CloudBox's VPC.

Limits

You cannot configure two or more lifecycle rules that contain a part policy for objects whose names contain overlapping prefixes. Examples:

  • Example 1

    If you configure a lifecycle rule that contains a part policy for an OSS on CloudBox bucket, you cannot configure another lifecycle rule that contains a part policy for any objects in the OSS on CloudBox bucket.

  • Example 2

    If you configure a lifecycle rule that contains a part policy for objects whose names contain the dir1 prefix in an OSS on CloudBox bucket, you cannot configure another lifecycle rule that contains a part policy for objects whose names contain overlapping prefixes, such as dir1/dir2.

Usage notes

Number of lifecycle rules

You can configure up to 100 lifecycle rules for an OSS on CloudBox bucket in the OSS console. To configure more than 100 lifecycle rules for an OSS on CloudBox bucket, use OSS SDKs or ossutil.

Overwrite semantics

The PutBucketLifecycle operation overwrites the existing configurations of a lifecycle rule of a bucket. For example, if a lifecycle rule named Rule1 is configured for a bucket and you want to configure another lifecycle rule named Rule2 for the bucket, perform the following operations:

  • Call the GetBucketLifecycle operation to query Rule1.

  • Add both Rule1 and Rule2 to the lifecycle rule configuration.

  • Call the PutBucketLifecycle operation to create Rule1 and Rule2 for the bucket.

Effective time

OSS loads a lifecycle rule within 24 hours after the rule is created. After the lifecycle rule is loaded, OSS runs the rule every day at 08:00:00 (UTC+8).

The interval between the last modified time of an object and the time when the lifecycle rule is run must be longer than 24 hours. For example, if you configure a lifecycle rule for a bucket to delete objects one day after they are uploaded, objects that are uploaded on July 20, 2020 are deleted on a different date based on the specific time when the objects are uploaded.

  • Objects uploaded before 08:00:00 (UTC+8) on July 20, 2020 are deleted from 08:00:00 (UTC+8) on July 21, 2020 to 08:00:00 (UTC+8) on July 22, 2020.

  • Objects uploaded after 08:00:00 (UTC+8) on July 20, 2020 are deleted from 08:00:00 (UTC+8) on July 22, 2020 to 08:00:00 (UTC+8) on July 23, 2020.

Important

When you update a lifecycle rule, tasks that are scheduled to be performed based on the rule on the current day are suspended. We recommend that you do not frequently update lifecycle rules.

Completion time

Completion time

Lifecycle rule that does not contain tags

  • For a lifecycle rule that is not based on tags, up to 1 billion lifecycle management actions, including object deletion, storage class conversion, and part expiration, can be completed within 24 hours in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), China (Zhangjiakou), China (Ulanqab), China (Shenzhen), and Singapore. The time required may be longer than 24 hours if the number of lifecycle-related operations exceeds one billion or if the total number of scanned objects exceeds 100 billion.

  • For a lifecycle rule that is not based on tags, up to 100 million lifecycle management actions can be completed within 24 hours in other regions. The time required may be longer than 24 hours if the number of lifecycle-related operations exceeds 100 million or if the total number of scanned objects exceeds 30 billion.

Lifecycle rule that contains tags

  • For a lifecycle rule that is based on tags, up to 500 million lifecycle management actions, including object deletion, storage class conversion, and part expiration, can be completed within 24 hours in the following regions: China (Hangzhou), China (Shanghai), China (Beijing), China (Zhangjiakou), China (Ulanqab), China (Shenzhen), and Singapore. The time required may be longer than 24 hours if the number of lifecycle-related operations exceeds 500 million, or if the number of objects scanned exceeds 100 billion or the number of tagging operations exceeds 10 billion.

  • For a lifecycle rule that is based on tags, up to 50 million lifecycle management actions can be completed within 24 hours in other regions. The time required may be longer than 24 hours if the number of lifecycle-related operations exceeds 50 million, the number of objects scanned exceeds 30 billion, or the number of tagging operations exceeds 3 billion.

Note

If versioning is enabled for a bucket, a lifecycle management action on each object version in the bucket is counted towards the applicable limit.

Billing rules

If you use lifecycle rules to convert the storage class of objects or delete objects, you may be charged request fees and storage fees. For more information, see Lifecycle Fees.

Procedure

Use the OSS console

  1. Log on to the OSS console.

  2. In the left-side navigation pane, choose Data Service > OSS on CloudBox Buckets.

  3. On the OSS on CloudBox Buckets page, click the OSS on CloudBox bucket for which you want to enable configure lifecycle rules.

  4. In the left-side navigation tree, choose Data Management > Lifecycle.

  5. On the Lifecycle page, click Create Rule. In the Create Rule panel, configure the parameters. The following table describes the parameters.

    • Unversioned OSS on CloudBox bucket

      Section

      Parameter

      Description

      Basic Settings

      Status

      Specify the status of the lifecycle rule. Valid values: Enabled and Disabled.

      Applied To

      Specify the objects to which the lifecycle rule applies. Valid values: Files with Specified Prefix and Whole Bucket.

      Prefix

      Specify the prefix in the names of the objects for which you want the lifecycle rule to take effect. For example, if you want the rule to take effect for objects whose names start with img, enter img.

      Clear Policy

      Object Lifecycle

      Configure rules for objects to specify when the objects expire. Valid values: Validity Period (Days), Expiration Date, and Disabled. If you select Disabled, the configurations of File Lifecycle do not take effect.

      Delete

      Delete expired objects.

      For example, if you set the Expiration Date parameter to Sept 24, 2022 and specify this parameter, objects that were last modified before September 24, 2022 are automatically deleted. Deleted objects cannot be recovered.

      Delete Parts

      Part Lifecycle

      Specify the operations that you want to perform on expired parts. You can select Validity Period (Days), Expiration Date, or Disabled. If you select Disabled, the part lifecycle configuration is not enabled.

      Important Each lifecycle rule must contain at least one of object expiration policies and part expiration policies.

      Delete

      Specify when parts expire based on the value of Part Lifecycle. Expired parts are automatically deleted and cannot be recovered.

    • Versioning-enabled OSS on CloudBox bucket

      Configure the parameters in the Basic Settings and Delete Parts sections in the same manner as you configure the parameters for an unversioned OSS on CloudBox bucket. The following table describes only the parameters that are different from the parameters that you configure for an unversioned OSS on CloudBox bucket.

      Section

      Parameter

      Description

      Current Version

      Removal of Delete Marker

      If you enable versioning for the bucket, you can configure the Clean Up Delete Marker parameter. Other parameters are the same as those you can configure for unversioned buckets.

      If you select Clean Up Delete Marker, and an object has only one version which is a delete marker, OSS considers the delete marker expired and removes the delete marker. If an object has multiple versions and the current version of the object is a delete marker, OSS retains the delete marker. For more information about delete makers, see Delete marker.

      Previous Versions

      Object Lifecycle

      Specify when previous versions expire. Valid values: Validity Period (Days) and Disabled. If you select Disabled, File Lifecycle does not take effect.

      Delete

      Specify the number of days for which objects can be retained after they become previous versions. After the previous versions expire, the previous versions are deleted the next day. For example, if you set Validity Period (Days) to 30, objects that became previous versions on September 1, 2021 are deleted on October 1, 2021.

      Important You can determine when an object becomes a previous version based on the time when the next version of the object is last modified.

Use an Alibaba Cloud SDK

You can configure lifecycle rules for an OSS on CloudBox bucket by using the OSS SDK for Java, Python, or Go (Java SDK 3.15.0 or later, Python SDK V2 1.1.1 or later, or Go SDK V2 v1.2.1 or later). For OSS on CloudBox, the Python and Go SDKs are called in the same way as they are in public cloud OSS. For information about how to create a client, see SDK support.

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.LifecycleRule;
import com.aliyun.oss.model.SetBucketLifecycleRequest;
import java.util.List;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Specify the data endpoint of the OSS on CloudBox bucket. 
        String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the OSS on CloudBox bucket. Example: examplebucket. 
        String bucketName = "examplebucket";
    // Specify the region in which the OSS on CloudBox bucket is located. 
    String region = "cn-hangzhou";
    // Specify the ID of the cloud box. 
        String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";

        // Create an OSSClient instance.
        // Call the shutdown method to release resources when the OSSClient is no longer in use. 
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
                .clientConfiguration(conf)
                .region(region)
                .build();

        try {
            // Create a request by using SetBucketLifecycleRequest. 
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);

            // Specify the ID of the lifecycle rule. 
            String ruleId0 = "rule0";
            // If the prefix is left empty, the lifecycle rule take effect for all objects in the OSS on CloudBox bucket. 
            String matchPrefix0 = null;

            // Specify that objects expire three days after they are last modified. 
            LifecycleRule rule = new LifecycleRule(ruleId0, matchPrefix0, LifecycleRule.RuleStatus.Enabled, 3);
            request.AddLifecycleRule(rule);

            // Initiate a request to configure the lifecycle rule. 
            ossClient.setBucketLifecycle(request);

            // Query the lifecycle rule. 
            List<LifecycleRule> listRules = ossClient.getBucketLifecycle(bucketName);
            for(LifecycleRule rules : listRules){
                System.out.println("ruleId="+rules.getId()+", matchPrefix="+rules.getPrefix()+", expirationDays="+rules.getExpirationDays());
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
import alibabacloud_oss_v2 as oss

# Obtain access credentials from environment variables. Before running this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration of the SDK.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region in which the CloudBox bucket is located, such as cn-hangzhou.
cfg.region = "cn-hangzhou"
# Specify the CloudBox ID. After you set the CloudBox ID, the SDK automatically switches the signing product to oss-cloudbox.
cfg.cloud_box_id = "cb-f8z7yvzgwfkl9q0h****"
# Specify the data domain of the CloudBox bucket in the CloudboxId.Region.oss-cloudbox.aliyuncs.com format.
cfg.endpoint = "cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com"
# Create an OSS client.
client = oss.Client(cfg)

# Configure a lifecycle rule that expires objects 3 days after they were last modified. The rule applies to all objects in the bucket.
result = client.put_bucket_lifecycle(oss.PutBucketLifecycleRequest(
    bucket="examplebucket",
    lifecycle_configuration=oss.LifecycleConfiguration(
        rules=[oss.LifecycleRule(
            id="rule0",
            prefix="",
            status="Enabled",
            expiration=oss.LifecycleRuleExpiration(
                days=3,
            ),
        )],
    ),
))
print(f'status code: {result.status_code}, request id: {result.request_id}')
package main

import (
	"context"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

func main() {
	// Use the default configuration of the SDK and obtain access credentials from environment variables.
	// Before running this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		// Specify the region in which the CloudBox bucket is located, such as cn-hangzhou.
		WithRegion("cn-hangzhou").
		// Specify the data domain of the CloudBox bucket in the CloudboxId.Region.oss-cloudbox.aliyuncs.com format.
		WithEndpoint("cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com").
		// Specify the CloudBox ID. After you set the CloudBox ID, the SDK automatically switches the signing product to oss-cloudbox.
		WithCloudBoxId("cb-f8z7yvzgwfkl9q0h****")

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Configure a lifecycle rule that expires objects 3 days after they were last modified. The rule applies to all objects in the bucket.
	request := &oss.PutBucketLifecycleRequest{
		Bucket: oss.Ptr("examplebucket"),
		LifecycleConfiguration: &oss.LifecycleConfiguration{
			Rules: []oss.LifecycleRule{
				{
					ID:     oss.Ptr("rule0"),
					Prefix: oss.Ptr(""),
					Status: oss.Ptr("Enabled"),
					Expiration: &oss.LifecycleRuleExpiration{
						Days: oss.Ptr(int32(3)),
					},
				},
			},
		},
	}
	result, err := client.PutBucketLifecycle(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket lifecycle %v", err)
	}
	log.Printf("put bucket lifecycle result:%#v\n", result)
}

Use ossutil

You can configure lifecycle rules for an OSS on CloudBox bucket by using ossutil. For more information, see put-bucket-lifecycle.

Use the RESTful API

If your business requires a high level of customization, you can directly call the RESTful APIs. To directly call an API, you must include the signature calculation in your code. For more information, see PutBucketLifecycle.