CompleteMultipartUploadRequest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Model.Tag;
  5. using COSXML.Common;
  6. using COSXML.CosException;
  7. using COSXML.Network;
  8. namespace COSXML.Model.Object
  9. {
  10. /// <summary>
  11. /// 实现完成整个分块上传
  12. /// <see href="https://cloud.tencent.com/document/product/436/7742"/>
  13. /// </summary>
  14. public sealed class CompleteMultipartUploadRequest : ObjectRequest
  15. {
  16. /// <summary>
  17. /// 本次分块上传的所有信息
  18. /// <see href="Model.Tag.CompleteMultipartUpload"/>
  19. /// </summary>
  20. private CompleteMultipartUpload completeMultipartUpload;
  21. /// <summary>
  22. /// 标识本次分块上传的 ID,
  23. /// 使用 Initiate Multipart Upload 接口初始化分片上传时会得到一个 uploadId,
  24. /// 该 ID 不但唯一标识这一分块数据,也标识了这分块数据在整个文件内的相对位置
  25. /// </summary>
  26. private string uploadId;
  27. public CompleteMultipartUploadRequest(string bucket, string key, string uploadId)
  28. : base(bucket, key)
  29. {
  30. this.method = CosRequestMethod.POST;
  31. this.uploadId = uploadId;
  32. completeMultipartUpload = new CompleteMultipartUpload();
  33. completeMultipartUpload.parts = new List<CompleteMultipartUpload.Part>();
  34. }
  35. /// <summary>
  36. /// 添加分片块(块编号,块ETag值)
  37. /// </summary>
  38. /// <param name="partNumber"></param>
  39. /// <param name="eTag"></param>
  40. public void SetPartNumberAndETag(int partNumber, string eTag)
  41. {
  42. CompleteMultipartUpload.Part part = new CompleteMultipartUpload.Part();
  43. part.partNumber = partNumber;
  44. part.eTag = eTag;
  45. completeMultipartUpload.parts.Add(part);
  46. }
  47. /// <summary>
  48. /// 添加分片块(块编号,块ETag值)
  49. /// </summary>
  50. /// <param name="partNumberAndETags"></param>
  51. public void SetPartNumberAndETag(Dictionary<int, string> partNumberAndETags)
  52. {
  53. if (partNumberAndETags != null)
  54. {
  55. foreach (KeyValuePair<int, string> pair in partNumberAndETags)
  56. {
  57. SetPartNumberAndETag(pair.Key, pair.Value);
  58. }
  59. }
  60. }
  61. public override Network.RequestBody GetRequestBody()
  62. {
  63. return GetXmlRequestBody(completeMultipartUpload);
  64. }
  65. public override void CheckParameters()
  66. {
  67. if (completeMultipartUpload.parts.Count == 0)
  68. {
  69. throw new CosClientException((int)CosClientError.InvalidArgument, "completeMultipartUpload.parts count = 0");
  70. }
  71. if (requestUrlWithSign != null)
  72. {
  73. return;
  74. }
  75. base.CheckParameters();
  76. if (uploadId == null)
  77. {
  78. throw new CosClientException((int)CosClientError.InvalidArgument, "uploadId is null");
  79. }
  80. }
  81. protected override void InternalUpdateQueryParameters()
  82. {
  83. try
  84. {
  85. this.queryParameters.Add("uploadId", uploadId);
  86. }
  87. catch (ArgumentException)
  88. {
  89. this.queryParameters["uploadId"] = uploadId;
  90. }
  91. }
  92. }
  93. }