PutBucketLifecycleRequest.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Common;
  5. using COSXML.Model.Tag;
  6. using COSXML.Network;
  7. using COSXML.CosException;
  8. namespace COSXML.Model.Bucket
  9. {
  10. /// <summary>
  11. /// 设置 Bucket 生命周期
  12. /// <see href="https://cloud.tencent.com/document/product/436/8280"/>
  13. /// </summary>
  14. public sealed class PutBucketLifecycleRequest : BucketRequest
  15. {
  16. private LifecycleConfiguration lifecycleConfiguration;
  17. public PutBucketLifecycleRequest(string bucket)
  18. : base(bucket)
  19. {
  20. this.method = CosRequestMethod.PUT;
  21. this.queryParameters.Add("lifecycle", null);
  22. lifecycleConfiguration = new LifecycleConfiguration();
  23. lifecycleConfiguration.rules = new List<LifecycleConfiguration.Rule>();
  24. }
  25. public override Network.RequestBody GetRequestBody()
  26. {
  27. return GetXmlRequestBody(lifecycleConfiguration);
  28. }
  29. /// <summary>
  30. /// 设置生命周期规则
  31. /// <see href="Model.Tag.LifecycleConfiguration.Rule"/>
  32. /// </summary>
  33. /// <param name="rule"></param>
  34. public void SetRule(LifecycleConfiguration.Rule rule)
  35. {
  36. if (rule != null)
  37. {
  38. lifecycleConfiguration.rules.Add(rule);
  39. }
  40. }
  41. /// <summary>
  42. /// 设置生命周期规则
  43. /// <see href="Model.Tag.LifecycleConfiguration.Rule"/>
  44. /// </summary>
  45. /// <param name="rules"></param>
  46. public void SetRules(List<LifecycleConfiguration.Rule> rules)
  47. {
  48. if (rules != null)
  49. {
  50. lifecycleConfiguration.rules.AddRange(rules);
  51. }
  52. }
  53. public override void CheckParameters()
  54. {
  55. base.CheckParameters();
  56. if (lifecycleConfiguration.rules.Count == 0)
  57. {
  58. throw new CosClientException((int)CosClientError.InvalidArgument, "lifecycleConfiguration.rules.Count = 0");
  59. }
  60. }
  61. }
  62. }