SetBucketLifecycleRequestSerializer.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System.IO;
  8. using Aliyun.OSS.Util;
  9. using Aliyun.OSS.Model;
  10. namespace Aliyun.OSS.Transform
  11. {
  12. internal class SetBucketLifecycleRequestSerializer : RequestSerializer<SetBucketLifecycleRequest, LifecycleConfiguration>
  13. {
  14. public SetBucketLifecycleRequestSerializer(ISerializer<LifecycleConfiguration, Stream> contentSerializer)
  15. : base(contentSerializer)
  16. { }
  17. public override Stream Serialize(SetBucketLifecycleRequest request)
  18. {
  19. var rules = request.LifecycleRules;
  20. var lcc = new LifecycleConfiguration
  21. {
  22. LifecycleRules = new Model.LifecycleRule[rules.Count]
  23. };
  24. for (var i = 0; i < rules.Count; i++)
  25. {
  26. lcc.LifecycleRules[i] = new Model.LifecycleRule
  27. {
  28. ID = rules[i].ID,
  29. Prefix = rules[i].Prefix
  30. };
  31. switch (rules[i].Status)
  32. {
  33. case RuleStatus.Enabled:
  34. lcc.LifecycleRules[i].Status = RuleStatus.Enabled.ToString();
  35. break;
  36. case RuleStatus.Disabled:
  37. lcc.LifecycleRules[i].Status = RuleStatus.Disabled.ToString();
  38. break;
  39. }
  40. lcc.LifecycleRules[i].Expiration = new Expiration();
  41. if (rules[i].ExpirationTime.HasValue)
  42. lcc.LifecycleRules[i].Expiration.Date = DateUtils.FormatIso8601Date(rules[i].ExpirationTime.Value);
  43. else if (rules[i].ExpriationDays.HasValue)
  44. lcc.LifecycleRules[i].Expiration.Days = rules[i].ExpriationDays.Value;
  45. }
  46. return ContentSerializer.Serialize(lcc);
  47. }
  48. }
  49. }