Not all data that is uploaded to Object Storage Service (OSS) buckets is frequently accessed. Rarely accessed data is stored in cold storage for compliance and archiving purposes. You may want to delete data that no longer needs to be stored in batches from your buckets based on your business scenario. In this case, you can configure lifecycle rules based on the last modification time of objects to periodically change the storage class of objects from hot to cold 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 OSS 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:PutBucketLifecycle
permission. To query lifecycle rules, you must have theoss:GetBucketLifecycle
permission. To clear lifecycle rules, you must have theoss:DeleteBucketLifecycle
permission. For more information, see Attach a custom policy to a RAM user.
Configure lifecycle rules for a bucket
The following code provides an example on how to configure lifecycle rules based on the last modification time of objects for a bucket named examplebucket. After you configure lifecycle rules, you can modify the lifecycle rules based on your business requirements. For information about how to modify existing lifecycle rules, see Lifecycle rules based on the last modified time.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
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");
/* Specify the tags of the objects that you want to match the rule. */
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 date. */
auto rule2 = LifecycleRule();
rule2.setID("rule2");
rule2.setPrefix("test2/");
rule2.setStatus(RuleStatus::Disabled);
rule2.setExpiration(date);
/* Specify that rule3 is enabled for the bucket if versioning is enabled for the bucket. */
auto rule3 = LifecycleRule();
rule3.setID("rule3");
rule3.setPrefix("test3/");
rule3.setStatus(RuleStatus::Disabled);
/* Specify that the storage classes of objects are changed to Archive 365 days after the objects are last modified. */
auto transition = LifeCycleTransition();
transition.Expiration().setDays(365);
transition.setStorageClass(StorageClass::Archive);
rule3.addTransition(transition);
/* Specify that expired delete markers are automatically deleted. */
rule3.setExpiredObjectDeleteMarker(true);
/* Specify that the storage classes of the previous versions of objects are changed to IA 10 days after the objects are last modified. */
auto transition1 = LifeCycleTransition();
transition1.Expiration().setDays(10);
transition1.setStorageClass(StorageClass::IA);
/* Specify that the storage classes of the previous versions of objects are changed to Archive 20 days after the objects are last modified. */
auto transition2 = LifeCycleTransition();
transition2.Expiration().setDays(20);
transition2.setStorageClass(StorageClass::Archive);
/* Specify that previous versions are deleted 30 days after the versions are updated. */
auto expiration = LifeCycleExpiration(30);
rule3.setNoncurrentVersionExpiration(expiration);
LifeCycleTransitionList noncurrentVersionStorageTransitions{transition1, transition2};
rule3.setNoncurrentVersionTransitionList(noncurrentVersionStorageTransitions);
/* Configure 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 resources such as network resources. */
ShutdownSdk();
return 0;
}
Query the lifecycle rules of a bucket
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 information about the account used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Query the lifecycle rules that are configured for the bucket. */
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;
}
/* Query the lifecycle rules to check whether expired delete markers are automatically deleted. */
if (rule.ExpiredObjectDeleteMarker()) {
std::cout << "rule expired delete marker: " << rule.ExpiredObjectDeleteMarker() << std::endl;
}
/* Query the configurations used to change the storage classes of the previous versions of the 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;
}
}
/* Query the expiration configurations for the previous versions of the 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 resources such as network resources. */
ShutdownSdk();
return 0;
}
Delete the lifecycle rules of a bucket
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 that are configured for a bucket?.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 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. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Delete 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 resources such as network resources. */
ShutdownSdk();
return 0;
}
References
For the complete sample code that is used to manage lifecycle rules, visit GitHub.
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.