PutBucketReplicationRequest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. public sealed class PutBucketReplicationRequest : BucketRequest
  11. {
  12. private ReplicationConfiguration replicationConfiguration;
  13. public PutBucketReplicationRequest(string bucket)
  14. : base(bucket)
  15. {
  16. this.method = CosRequestMethod.PUT;
  17. this.queryParameters.Add("replication", null);
  18. replicationConfiguration = new ReplicationConfiguration();
  19. replicationConfiguration.rules = new List<ReplicationConfiguration.Rule>();
  20. }
  21. public override Network.RequestBody GetRequestBody()
  22. {
  23. return GetXmlRequestBody(replicationConfiguration);
  24. }
  25. public void SetReplicationConfiguration(string ownerUin, string subUin, List<RuleStruct> ruleStructs)
  26. {
  27. SetReplicationConfigurationWithRole(ownerUin, subUin);
  28. if (ruleStructs != null)
  29. {
  30. foreach (RuleStruct ruleStruct in ruleStructs)
  31. {
  32. SetReplicationConfigurationWithRule(ruleStruct);
  33. }
  34. }
  35. }
  36. private void SetReplicationConfigurationWithRole(string ownerUin, string subUin)
  37. {
  38. if (ownerUin != null && subUin != null)
  39. {
  40. string role = "qcs::cam::uin/" + ownerUin + ":uin/" + subUin;
  41. replicationConfiguration.role = role;
  42. }
  43. }
  44. private void SetReplicationConfigurationWithRule(RuleStruct ruleStruct)
  45. {
  46. if (ruleStruct != null)
  47. {
  48. ReplicationConfiguration.Rule rule = new ReplicationConfiguration.Rule();
  49. rule.id = ruleStruct.id;
  50. rule.status = ruleStruct.isEnable ? "Enabled" : "Disabled";
  51. rule.prefix = ruleStruct.prefix;
  52. ReplicationConfiguration.Destination destination = new ReplicationConfiguration.Destination();
  53. destination.storageClass = ruleStruct.storageClass;
  54. string bucketName = ruleStruct.bucket;
  55. if (ruleStruct.bucket.EndsWith("-" + ruleStruct.appid))
  56. {
  57. bucketName = ruleStruct.bucket.Replace("-" + ruleStruct.appid, "");
  58. }
  59. StringBuilder bucket = new StringBuilder();
  60. bucket.Append("qcs:id/0:cos:").Append(ruleStruct.region).Append(":appid/")
  61. .Append(ruleStruct.appid).Append(":").Append(bucketName);
  62. destination.bucket = bucket.ToString();
  63. rule.destination = destination;
  64. replicationConfiguration.rules.Add(rule);
  65. }
  66. }
  67. public override void CheckParameters()
  68. {
  69. base.CheckParameters();
  70. if (replicationConfiguration.rules.Count == 0)
  71. {
  72. throw new CosClientException((int)CosClientError.InvalidArgument, "replicationConfiguration.rules.Count = 0");
  73. }
  74. }
  75. public sealed class RuleStruct
  76. {
  77. public string appid;
  78. public string region;
  79. public string bucket;
  80. public string storageClass;
  81. public string id;
  82. public string prefix;
  83. public bool isEnable;
  84. }
  85. }
  86. }