Object Storage Service (OSS) allows you to configure lifecycle rules to delete expired objects and parts or convert the storage class of expired objects to Infrequent Access (IA) or Archive to reduce your storage costs. This topic describes how to manage lifecycle rules for buckets.
Usage notes
- In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
- In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or STS, see Initialization.
- The
oss:PutBucketLifecycle
permission is required to configure a lifecycle rule. Theoss:GetBucketLifecycle
permission is required to query a lifecycle rule. Theoss:DeleteBucketLifecycle
permission is required to delete a lifecycle rule. For more information, see Attach a custom policy to a RAM user.
Configure lifecycle rules
The following code provides an example on how to configure a lifecycle rule based on the last modified time for a bucket named examplebucket. The lifecycle rule matches objects by prefix and tag. The storage class of the matched objects is converted, or the matched objects are deleted based on the lifecycle rule.
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
var setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucketName);
// Create the first lifecycle rule.
LifecycleRule lcr1 = new LifecycleRule()
{
ID = "delete obsoleted files",
Prefix = "obsoleted/",
Status = RuleStatus.Enabled,
ExpriationDays = 3,
Tags = new Tag[1]
};
// Specify tags for the first rule.
var tag1 = new Tag
{
Key = "project",
Value = "projectone"
};
lcr1.Tags[0] = tag1;
// Create the second lifecycle rule.
LifecycleRule lcr2 = new LifecycleRule()
{
ID = "delete temporary files",
Prefix = "temporary/",
Status = RuleStatus.Enabled,
ExpriationDays = 20,
Tags = new Tag[1]
};
// Specify tags for the second rule.
var tag2 = new Tag
{
Key = "user",
Value = "jsmith"
};
lcr2.Tags[0] = tag2;
// Specify that parts expire 30 days after they are last modified.
lcr2.AbortMultipartUpload = new LifecycleRule.LifeCycleExpiration()
{
Days = 30
};
LifecycleRule lcr3 = new LifecycleRule();
lcr3.ID = "only NoncurrentVersionTransition";
lcr3.Prefix = "test1";
lcr3.Status = RuleStatus.Enabled;
lcr3.NoncurrentVersionTransitions = new LifecycleRule.LifeCycleNoncurrentVersionTransition[2]
{
// Specify that the storage class of the previous versions of objects is converted to IA 90 days after they are last modified.
new LifecycleRule.LifeCycleNoncurrentVersionTransition(){
StorageClass = StorageClass.IA,
NoncurrentDays = 90
},
// Specify that the storage class of the previous versions of objects is converted to Archive 180 days after they are last modified.
new LifecycleRule.LifeCycleNoncurrentVersionTransition(){
StorageClass = StorageClass.Archive,
NoncurrentDays = 180
}
};
setBucketLifecycleRequest.AddLifecycleRule(lcr1);
setBucketLifecycleRequest.AddLifecycleRule(lcr2);
setBucketLifecycleRequest.AddLifecycleRule(lcr3);
// Configure lifecycle rules.
client.SetBucketLifecycle(setBucketLifecycleRequest);
Console.WriteLine("Set bucket:{0} Lifecycle succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
Query lifecycle rules
The following code provides an example on how to query the lifecycle rules configured for the bucket named examplebucket:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// Query the lifecycle rules of the bucket.
var rules = client.GetBucketLifecycle(bucketName);
Console.WriteLine("Get bucket:{0} Lifecycle succeeded ", bucketName);
foreach (var rule in rules)
{
Console.WriteLine("ID: {0}", rule.ID);
Console.WriteLine("Prefix: {0}", rule.Prefix);
Console.WriteLine("Status: {0}", rule.Status);
// Query the tags that are specified for the lifecycle rules.
foreach (var tag in rule.Tags)
{
Console.WriteLine("key:{0}, value:{1}", tag.Key, tag.Value);
}
// View the expiration rule for the previous versions of objects.
foreach (var version in rule.NoncurrentVersionTransitions)
{
Console.WriteLine("expiration day:{0}, storage class:{1}", version.NoncurrentDays, version.StorageClass);
}
if (rule.ExpriationDays.HasValue)
Console.WriteLine("ExpirationDays: {0}", rule.ExpriationDays);
}
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
Delete lifecycle rules
The following code provides an example on how to delete lifecycle rules configured for the bucket named examplebucket:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// Delete the lifecycle rules.
client.DeleteBucketLifecycle(bucketName);
Console.WriteLine("Delete bucket:{0} Lifecycle succeeded ", bucketName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
References
- For the complete sample code for lifecycle rules, visit GitHub.
- For more information about the API operation that you can call to configure lifecycle rules, 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.