DeleteMultiObjectRequest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Model.Tag;
  5. using COSXML.Common;
  6. using COSXML.Network;
  7. using COSXML.CosException;
  8. namespace COSXML.Model.Object
  9. {
  10. /// <summary>
  11. /// 实现在指定 Bucket 中批量删除 Object,单次请求最大支持批量删除 1000 个 Object
  12. /// <see href="https://cloud.tencent.com/document/product/436/8289"/>
  13. /// </summary>
  14. public sealed class DeleteMultiObjectRequest : ObjectRequest
  15. {
  16. /// <summary>
  17. /// 删除对象
  18. /// <see href="Model.Tag.Delete"/>
  19. /// </summary>
  20. private Delete delete;
  21. public DeleteMultiObjectRequest(string bucket)
  22. : base(bucket, "/")
  23. {
  24. this.method = CosRequestMethod.POST;
  25. this.queryParameters.Add("delete", null);
  26. delete = new Delete();
  27. delete.deleteObjects = new List<Delete.DeleteObject>();
  28. }
  29. /// <summary>
  30. /// 返回模式
  31. /// verbose 模式和 quiet 模式,默认为 verbose 模式。
  32. /// verbose 模式返回每个 key 的删除情况,quiet 模式只返回删除失败的 key 的情况;
  33. /// </summary>
  34. /// <param name="quiet"></param>
  35. public void SetDeleteQuiet(bool quiet)
  36. {
  37. delete.quiet = quiet;
  38. }
  39. /// <summary>
  40. /// 删除对象
  41. /// </summary>
  42. /// <param name="key"></param>
  43. public void SetDeleteKey(string key)
  44. {
  45. SetDeleteKey(key, null);
  46. }
  47. /// <summary>
  48. /// 删除指定版本的对象
  49. /// </summary>
  50. /// <param name="key"></param>
  51. /// <param name="versionId"></param>
  52. public void SetDeleteKey(string key, string versionId)
  53. {
  54. if (!String.IsNullOrEmpty(key))
  55. {
  56. if (key.StartsWith("/"))
  57. {
  58. key = key.Substring(1);
  59. }
  60. Delete.DeleteObject deleteObject = new Delete.DeleteObject();
  61. deleteObject.key = key;
  62. if (versionId != null)
  63. {
  64. deleteObject.versionId = versionId;
  65. }
  66. delete.deleteObjects.Add(deleteObject);
  67. }
  68. }
  69. /// <summary>
  70. /// 删除对象
  71. /// </summary>
  72. /// <param name="keys"></param>
  73. public void SetObjectKeys(List<string> keys)
  74. {
  75. if (keys != null)
  76. {
  77. foreach (string key in keys)
  78. {
  79. SetDeleteKey(key, null);
  80. }
  81. }
  82. }
  83. public override void CheckParameters()
  84. {
  85. if (delete.deleteObjects.Count == 0)
  86. {
  87. throw new CosClientException((int)CosClientError.InvalidArgument, "delete keys(null or empty) is invalid");
  88. }
  89. base.CheckParameters();
  90. }
  91. public override Network.RequestBody GetRequestBody()
  92. {
  93. return GetXmlRequestBody(delete);
  94. }
  95. }
  96. }