PutBucketCORSRequest.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 CORS
  12. /// <see href="https://cloud.tencent.com/document/product/436/8279"/>
  13. /// </summary>
  14. public sealed class PutBucketCORSRequest : BucketRequest
  15. {
  16. /// <summary>
  17. /// CORS 配置信息
  18. /// <see href="Model.Tag.CORSConfiguration"/>
  19. /// </summary>
  20. private CORSConfiguration corsConfiguration;
  21. public PutBucketCORSRequest(string bucket)
  22. : base(bucket)
  23. {
  24. this.method = CosRequestMethod.PUT;
  25. this.queryParameters.Add("cors", null);
  26. corsConfiguration = new CORSConfiguration();
  27. corsConfiguration.corsRules = new List<CORSConfiguration.CORSRule>();
  28. }
  29. public override Network.RequestBody GetRequestBody()
  30. {
  31. return GetXmlRequestBody(corsConfiguration);
  32. }
  33. /// <summary>
  34. /// 设置 CORS 规则
  35. /// <see href="Model.Tag.CORSConfiguration.CORSRule"/>
  36. /// </summary>
  37. /// <param name="corsRule"></param>
  38. public void SetCORSRule(CORSConfiguration.CORSRule corsRule)
  39. {
  40. if (corsRule != null)
  41. {
  42. corsConfiguration.corsRules.Add(corsRule);
  43. }
  44. }
  45. /// <summary>
  46. /// 设置 CORS 规则
  47. /// <see href="Model.Tag.CORSConfiguration.CORSRule"/>
  48. /// </summary>
  49. /// <param name="corsRules"></param>
  50. public void SetCORSRules(List<CORSConfiguration.CORSRule> corsRules)
  51. {
  52. if (corsRules != null)
  53. {
  54. corsConfiguration.corsRules.AddRange(corsRules);
  55. }
  56. }
  57. public override void CheckParameters()
  58. {
  59. base.CheckParameters();
  60. if (corsConfiguration.corsRules.Count == 0)
  61. {
  62. throw new CosClientException((int)CosClientError.InvalidArgument, "corsConfiguration.corsRules.Count = 0");
  63. }
  64. }
  65. }
  66. }