Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

使用适用于 .NET 的 AWS 开发工具包管理对象生命周期

您可以使用管理适用于 .NET 的 AWS 开发工具包存储桶上的生命周期配置。有关管理生命周期配置的更多信息,请参阅 对象生命周期管理

例 .NET 代码示例

以下 C# 代码示例向存储桶添加生命周期配置。该示例显示两种生命周期配置:

  • 仅使用前缀选择规则将应用于的一部分对象的生命周期配置。

  • 使用前缀和对象标签选择规则将应用于的一部分对象的生命周期配置。

创建对象后,生命周期规则会立即将对象转换为 GLACIER 存储类。

以下代码适用于最新版本的 .NET 开发工具包。

有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

using System; using System.Collections.Generic; using System.Diagnostics; using Amazon.S3; using Amazon.S3.Model; namespace aws.amazon.com.s3.documentation { class LifeCycleTest { static string bucketName = "*** bucket name ***"; public static void Main(string[] args) { try { using (var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { // 1. Add lifecycle config with prefix only. var lifeCycleConfigurationA = LifecycleConfig1(); // Add the configuration to the bucket PutLifeCycleConfiguration(client, lifeCycleConfigurationA); // Retrieve an existing configuration var lifeCycleConfiguration = GetLifeCycleConfiguration(client); // 2. Add lifecycle config with prefix and tags. var lifeCycleConfigurationB = LifecycleConfig2(); // Add the configuration to the bucket PutLifeCycleConfiguration(client, lifeCycleConfigurationB); // Retrieve an existing configuration lifeCycleConfiguration = GetLifeCycleConfiguration(client); // 3. Delete lifecycle config. DeleteLifecycleConfiguration(client); // 4. Retrieve a nonexistent configuration lifeCycleConfiguration = GetLifeCycleConfiguration(client); Debug.Assert(lifeCycleConfiguration == null); } Console.WriteLine("Example complete. To continue, click Enter..."); Console.ReadKey(); } catch (AmazonS3Exception amazonS3Exception) { Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception.ToString()); } catch (Exception e) { Console.WriteLine("Exception: " + e.ToString()); } } private static LifecycleConfiguration LifecycleConfig1() { var lifeCycleConfiguration = new LifecycleConfiguration() { Rules = new List<LifecycleRule> { new LifecycleRule { Id = "Rule-1", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecyclePrefixPredicate { Prefix = "glacier/" } }, Status = LifecycleRuleStatus.Enabled, Transitions = new List<LifecycleTransition> { new LifecycleTransition { Days = 0, StorageClass = S3StorageClass.Glacier } }, } } }; return lifeCycleConfiguration; } private static LifecycleConfiguration LifecycleConfig2() { var lifeCycleConfiguration = new LifecycleConfiguration() { Rules = new List<LifecycleRule> { new LifecycleRule { Id = "Rule-1", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecycleAndOperator { Operands = new List<LifecycleFilterPredicate> { new LifecyclePrefixPredicate { Prefix = "glacierobjects/" }, new LifecycleTagPredicate { Tag = new Tag() { Key = "tagKey1", Value = "tagValue1" } }, new LifecycleTagPredicate { Tag = new Tag() { Key = "tagKey2", Value = "tagValue2" } } } } }, Status = LifecycleRuleStatus.Enabled, Transitions = new List<LifecycleTransition> { new LifecycleTransition { Days = 0, StorageClass = S3StorageClass.Glacier } }, } } }; return lifeCycleConfiguration; } static void PutLifeCycleConfiguration(IAmazonS3 client, LifecycleConfiguration configuration) { PutLifecycleConfigurationRequest request = new PutLifecycleConfigurationRequest { BucketName = bucketName, Configuration = configuration }; var response = client.PutLifecycleConfiguration(request); } static LifecycleConfiguration GetLifeCycleConfiguration(IAmazonS3 client) { GetLifecycleConfigurationRequest request = new GetLifecycleConfigurationRequest { BucketName = bucketName }; var response = client.GetLifecycleConfiguration(request); var configuration = response.Configuration; return configuration; } static void DeleteLifecycleConfiguration(IAmazonS3 client) { DeleteLifecycleConfigurationRequest request = new DeleteLifecycleConfigurationRequest { BucketName = bucketName }; client.DeleteLifecycleConfiguration(request); } } }