Amazon Simple Storage Service
开发人员指南 (API 版本 2006-03-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

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

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

注意

在添加生命周期配置时,Amazon S3 将替换指定存储桶上的现有生命周期配置。要更新配置,您必须先检索生命周期配置,进行更改,然后向存储桶添加已修订的生命周期配置。

例 .NET 代码示例

以下示例说明如何使用适用于 .NET 的 AWS 开发工具包添加、更新和删除存储桶的生命周期配置。该代码示例执行以下操作:

  • 向存储桶添加生命周期配置。

  • 检索生命周期配置并通过添加其他规则来更新该配置。

  • 向存储桶添加已修改的生命周期配置。Amazon S3 将替换现有生命周期配置。

  • 再次检索配置并通过输出配置中的规则数进行验证。

  • 删除生命周期配置并确认删除操作

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

// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.) using Amazon.S3; using Amazon.S3.Model; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class LifecycleTest { private const string bucketName = "*** bucket name ***"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 client; public static void Main() { client = new AmazonS3Client(bucketRegion); AddUpdateDeleteLifecycleConfigAsync().Wait(); } private static async Task AddUpdateDeleteLifecycleConfigAsync() { try { var lifeCycleConfiguration = new LifecycleConfiguration() { Rules = new List<LifecycleRule> { new LifecycleRule { Id = "Archive immediately rule", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecyclePrefixPredicate() { Prefix = "glacierobjects/" } }, Status = LifecycleRuleStatus.Enabled, Transitions = new List<LifecycleTransition> { new LifecycleTransition { Days = 0, StorageClass = S3StorageClass.Glacier } }, }, new LifecycleRule { Id = "Archive and then delete rule", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecyclePrefixPredicate() { Prefix = "projectdocs/" } }, Status = LifecycleRuleStatus.Enabled, Transitions = new List<LifecycleTransition> { new LifecycleTransition { Days = 30, StorageClass = S3StorageClass.StandardInfrequentAccess }, new LifecycleTransition { Days = 365, StorageClass = S3StorageClass.Glacier } }, Expiration = new LifecycleRuleExpiration() { Days = 3650 } } } }; // Add the configuration to the bucket. await AddExampleLifecycleConfigAsync(client, lifeCycleConfiguration); // Retrieve an existing configuration. lifeCycleConfiguration = await RetrieveLifecycleConfigAsync(client); // Add a new rule. lifeCycleConfiguration.Rules.Add(new LifecycleRule { Id = "NewRule", Filter = new LifecycleFilter() { LifecycleFilterPredicate = new LifecyclePrefixPredicate() { Prefix = "YearlyDocuments/" } }, Expiration = new LifecycleRuleExpiration() { Days = 3650 } }); // Add the configuration to the bucket. await AddExampleLifecycleConfigAsync(client, lifeCycleConfiguration); // Verify that there are now three rules. lifeCycleConfiguration = await RetrieveLifecycleConfigAsync(client); Console.WriteLine("Expected # of rulest=3; found:{0}", lifeCycleConfiguration.Rules.Count); // Delete the configuration. await RemoveLifecycleConfigAsync(client); // Retrieve a nonexistent configuration. lifeCycleConfiguration = await RetrieveLifecycleConfigAsync(client); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } } static async Task AddExampleLifecycleConfigAsync(IAmazonS3 client, LifecycleConfiguration configuration) { PutLifecycleConfigurationRequest request = new PutLifecycleConfigurationRequest { BucketName = bucketName, Configuration = configuration }; var response = await client.PutLifecycleConfigurationAsync(request); } static async Task<LifecycleConfiguration> RetrieveLifecycleConfigAsync(IAmazonS3 client) { GetLifecycleConfigurationRequest request = new GetLifecycleConfigurationRequest { BucketName = bucketName }; var response = await client.GetLifecycleConfigurationAsync(request); var configuration = response.Configuration; return configuration; } static async Task RemoveLifecycleConfigAsync(IAmazonS3 client) { DeleteLifecycleConfigurationRequest request = new DeleteLifecycleConfigurationRequest { BucketName = bucketName }; await client.DeleteLifecycleConfigurationAsync(request); } } }