OSS支持设置生命周期(Lifecycle)规则,自动删除过期的文件和碎片,或将到期的文件转储为低频或归档存储类型,从而节省存储费用。本文介绍如何管理生命周期规则。
说明 关于生命周期的更多信息,请参见开发指南中的
生命周期规则介绍。
设置生命周期规则
以下代码用于设置生命周期规则:
using Aliyun.OSS; using Aliyun.OSS.Common; var endpoint = "<yourEndpoint>"; var accessKeyId = "<yourAccessKeyId>"; var accessKeySecret = "<yourAccessKeySecret>"; var bucketName = "<yourBucketName>"; // 创建OSSClient实例。 var client = new OssClient(endpoint, accessKeyId, accessKeySecret); try { var setBucketLifecycleRequest = new SetBucketLifecycleRequest(bucketName); // 创建第1条生命周期规则。 LifecycleRule lcr1 = new LifecycleRule() { ID = "delete obsoleted files", Prefix = "obsoleted/", Status = RuleStatus.Enabled, ExpriationDays = 3, Tags = new Tag[1] }; // 设置标签。 var tag1 = new Tag { Key = "project", Value = "projectone" }; lcr1.Tags[0] = tag1; // 创建第2条生命周期规则。 LifecycleRule lcr2 = new LifecycleRule() { ID = "delete temporary files", Prefix = "temporary/", Status = RuleStatus.Enabled, ExpriationDays = 20, Tags = new Tag[1] }; // 设置标签。 var tag2 = new Tag { Key = "user", Value = "jsmith" }; lcr2.Tags[0] = tag2; // 设置碎片在距最后修改时间30天后过期。 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] { // 设置非当前版本的Object距最后修改时间90天之后转为低频访问类型。 new LifecycleRule.LifeCycleNoncurrentVersionTransition(){ StorageClass = StorageClass.IA, NoncurrentDays = 90 }, // 设置非当前版本的Object距最后修改时间180天之后转为归档类型。 new LifecycleRule.LifeCycleNoncurrentVersionTransition(){ StorageClass = StorageClass.Archive, NoncurrentDays = 180 } }; setBucketLifecycleRequest.AddLifecycleRule(lcr1); setBucketLifecycleRequest.AddLifecycleRule(lcr2); setBucketLifecycleRequest.AddLifecycleRule(lcr3); // 设置生命周期规则。 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); }
查看生命周期规则
以下代码用于查看生命周期规则:
using Aliyun.OSS; using Aliyun.OSS.Common; var endpoint = "<yourEndpoint>"; var accessKeyId = "<yourAccessKeyId>"; var accessKeySecret = "<yourAccessKeySecret>"; var bucketName = "<yourBucketName>"; // 创建OSSClient实例。 var client = new OssClient(endpoint, accessKeyId, accessKeySecret); try { // 查看生命周期规则。 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); // 查看标签信息。 foreach (var tag in rule.Tags) { Console.WriteLine("key:{0}, value:{1}", tag.Key, tag.Value); } // 查看非当前版本Object过期规则。 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); }
清空生命周期规则
以下代码用于清空生命周期规则:
using Aliyun.OSS; using Aliyun.OSS.Common; var endpoint = "<yourEndpoint>"; var accessKeyId = "<yourAccessKeyId>"; var accessKeySecret = "<yourAccessKeySecret>"; var bucketName = "<yourBucketName>"; // 创建OSSClient实例。 var client = new OssClient(endpoint, accessKeyId, accessKeySecret); try { // 清空生命周期规则。 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); }