Not all data uploaded to OSS requires frequent access. For data compliance or archival purposes, some data must be stored in a cold storage class. In other scenarios, you may want to batch delete data that is no longer needed in a bucket. You can configure lifecycle rules based on the last modified time. These rules periodically transition objects to colder storage classes or delete objects to reduce storage costs.
Usage notes
Before you configure lifecycle rules based on the last modified time of objects, make sure that you familiarize yourself with this feature. For more information, see Lifecycle rules based on the last modified time.
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To configure lifecycle rules, you must have the
oss:PutBucketLifecyclepermission. To query lifecycle rules, you must have theoss:GetBucketLifecyclepermission. To clear lifecycle rules, you must have theoss:DeleteBucketLifecyclepermission. For more information, see Attach a custom policy to a RAM user.
Set lifecycle rules
The following code provides an example of how to set lifecycle rules for the `examplebucket` bucket based on the last modified time. If you want to modify one or more rules after they are set, see How do I modify one or more lifecycle rules?
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
SetBucketLifecycleRequest request(BucketName);
std::string date("2022-10-12T00:00:00.000Z");
/* Set tags. */
Tagging tagging;
tagging.addTag(Tag("key1", "value1"));
tagging.addTag(Tag("key2", "value2"));
/* Specify a lifecycle rule. */
auto rule1 = LifecycleRule();
rule1.setID("rule1");
rule1.setPrefix("test1/");
rule1.setStatus(RuleStatus::Enabled);
rule1.setExpiration(3);
rule1.setTags(tagging.Tags());
/* Specify the expiration time. */
auto rule2 = LifecycleRule();
rule2.setID("rule2");
rule2.setPrefix("test2/");
rule2.setStatus(RuleStatus::Disabled);
rule2.setExpiration(date);
/* rule3 is a lifecycle rule for a bucket with versioning enabled. */
auto rule3 = LifecycleRule();
rule3.setID("rule3");
rule3.setPrefix("test3/");
rule3.setStatus(RuleStatus::Disabled);
/* Transition objects to the Archive storage class 365 days after they are last modified. */
auto transition = LifeCycleTransition();
transition.Expiration().setDays(365);
transition.setStorageClass(StorageClass::Archive);
rule3.addTransition(transition);
/* Automatically remove expired delete markers. */
rule3.setExpiredObjectDeleteMarker(true);
/* Transition noncurrent versions of objects to the Infrequent Access storage class 10 days after they become noncurrent. */
auto transition1 = LifeCycleTransition();
transition1.Expiration().setDays(10);
transition1.setStorageClass(StorageClass::IA);
/* Transition noncurrent versions of objects to the Archive storage class 20 days after they become noncurrent. */
auto transition2 = LifeCycleTransition();
transition2.Expiration().setDays(20);
transition2.setStorageClass(StorageClass::Archive);
/* Delete objects 30 days after they become noncurrent versions. */
auto expiration = LifeCycleExpiration(30);
rule3.setNoncurrentVersionExpiration(expiration);
LifeCycleTransitionList noncurrentVersionStorageTransitions{transition1, transition2};
rule3.setNoncurrentVersionTransitionList(noncurrentVersionStorageTransitions);
/* Set the lifecycle rules. */
LifecycleRuleList list{rule1, rule2, rule3};
request.setLifecycleRules(list);
auto outcome = client.SetBucketLifecycle(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "SetBucketLifecycle fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}View lifecycle rules
The following code provides an example on how to query the lifecycle rules configured for the bucket named examplebucket:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
std::string ToStorageClassName(const StorageClass& storageClass) {
switch (storageClass) {
case StorageClass::Standard:
return "Standard";
case StorageClass::IA:
return "IA";
case StorageClass::Archive:
return "Archive";
case StorageClass::ColdArchive:
return "ColdArchive";
default:
return "Unknown";
}
}
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* View the lifecycle rules. */
auto outcome = client.GetBucketLifecycle(BucketName);
if (outcome.isSuccess()) {
std::cout << "GetBucketLifecycle success," << std::endl;
for (auto const rule : outcome.result().LifecycleRules()) {
std::cout << "rule:" << rule.ID() << "," << rule.Prefix() << "," << rule.Status() << ","
"hasExpiration:" << rule.hasExpiration() << "," <<
"hasTransitionList:" << rule.hasTransitionList() << "," << std::endl;
auto taglist = rule.Tags();
for (const auto& tag : taglist)
{
std::cout << "GetBucketLifecycle tag success, Key:"
<< tag.Key() << "; Value:" << tag.Value() << std::endl;
}
/* Check whether to automatically remove expired delete markers. */
if (rule.ExpiredObjectDeleteMarker()) {
std::cout << "rule expired delete marker: " << rule.ExpiredObjectDeleteMarker() << std::endl;
}
/* View the storage class transition rules for noncurrent versions of objects. */
if (rule.hasNoncurrentVersionTransitionList()) {
for (auto const lifeCycleTransition : rule.NoncurrentVersionTransitionList()) {
std::cout << "rule noncurrent versions trans days:" << std::to_string(lifeCycleTransition.Expiration().Days()) <<
" trans storage class: " << ToStorageClassName(lifeCycleTransition.StorageClass()) << std::endl;
}
}
/* View the expiration rules for noncurrent versions of objects. */
if (rule.hasNoncurrentVersionExpiration()) {
std::cout << "rule noncurrent versions expiration days:" << rule.NoncurrentVersionExpiration().Days() << std::endl;
}
}
}
else {
/* Handle exceptions. */
std::cout << "GetBucketLifecycle fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Clear lifecycle rules
The following code provides an example on how to clear lifecycle rules in a bucket named examplebucket. If you want to delete one or more lifecycle rules, refer to How do I delete one or more lifecycle rules?.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Clear the lifecycle rules. */
DeleteBucketLifecycleRequest request(BucketName);
auto outcome = client.DeleteBucketLifecycle(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "DeleteBucketLifecycle fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}References
For the complete sample code for lifecycle rules, see GitHub sample.
For more information about the API operation that you can call to configure a lifecycle rule, see PutBucketLifecycle.
For more information about the API operation that you can call to query lifecycle rules, see GetBucketLifecycle.
For more information about the API operation that you can call to delete lifecycle rules, see DeleteBucketLifecycle.