UploadPartRequest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Common;
  5. using System.IO;
  6. using COSXML.Log;
  7. using COSXML.CosException;
  8. using COSXML.Network;
  9. namespace COSXML.Model.Object
  10. {
  11. /// <summary>
  12. /// 分片上传
  13. /// <see href="https://cloud.tencent.com/document/product/436/7750"/>
  14. /// </summary>
  15. public sealed class UploadPartRequest : ObjectRequest
  16. {
  17. private static string TAG = typeof(UploadPartRequest).FullName;
  18. /// <summary>
  19. /// 分片块编号
  20. /// </summary>
  21. private int partNumber;
  22. /// <summary>
  23. /// 分片上传的UploadId
  24. /// </summary>
  25. private string uploadId;
  26. /// <summary>
  27. /// 本地文件路径
  28. /// </summary>
  29. private string srcPath;
  30. /// <summary>
  31. /// 上传文件指定起始位置
  32. /// </summary>
  33. private long fileOffset = -1L;
  34. /// <summary>
  35. /// 上传指定内容的长度
  36. /// </summary>
  37. private long contentLength = -1L;
  38. /// <summary>
  39. /// 上传回调
  40. /// </summary>
  41. private COSXML.Callback.OnProgressCallback progressCallback;
  42. private UploadPartRequest(string bucket, string key, int partNumber, string uploadId)
  43. : base(bucket, key)
  44. {
  45. this.method = CosRequestMethod.PUT;
  46. this.partNumber = partNumber;
  47. this.uploadId = uploadId;
  48. }
  49. /// <summary>
  50. /// 上传文件的指定内容
  51. /// </summary>
  52. /// <param name="bucket"></param>
  53. /// <param name="key"></param>
  54. /// <param name="partNumber"></param>
  55. /// <param name="uploadId"></param>
  56. /// <param name="srcPath"></param>
  57. /// <param name="fileOffset">文件指定起始位置</param>
  58. /// <param name="fileSendLength">文件指定内容长度</param>
  59. public UploadPartRequest(string bucket, string key, int partNumber, string uploadId, string srcPath, long fileOffset,
  60. long fileSendLength)
  61. : this(bucket, key, partNumber, uploadId)
  62. {
  63. this.srcPath = srcPath;
  64. this.fileOffset = fileOffset < 0 ? 0 : fileOffset;
  65. this.contentLength = fileSendLength < 0 ? -1L : fileSendLength;
  66. }
  67. /// <summary>
  68. /// 最大上传速度,单位是 bit/s
  69. /// </summary>
  70. /// <param name="rate"></param>
  71. public void LimitTraffic(long rate)
  72. {
  73. SetRequestHeader(CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT, rate.ToString());
  74. }
  75. /// <summary>
  76. /// 设置回调
  77. /// </summary>
  78. /// <param name="progressCallback"></param>
  79. public void SetCosProgressCallback(COSXML.Callback.OnProgressCallback progressCallback)
  80. {
  81. this.progressCallback = progressCallback;
  82. }
  83. public override void CheckParameters()
  84. {
  85. if (srcPath == null)
  86. {
  87. throw new CosClientException((int)(CosClientError.InvalidArgument), "data source = null");
  88. }
  89. if (srcPath != null)
  90. {
  91. if (!File.Exists(srcPath))
  92. {
  93. throw new CosClientException((int)(CosClientError.InvalidArgument), "file not exist");
  94. }
  95. }
  96. if (requestUrlWithSign != null)
  97. {
  98. return;
  99. }
  100. base.CheckParameters();
  101. if (partNumber <= 0)
  102. {
  103. throw new CosClientException((int)CosClientError.InvalidArgument, "partNumber < 1");
  104. }
  105. if (uploadId == null)
  106. {
  107. throw new CosClientException((int)CosClientError.InvalidArgument, "uploadId = null");
  108. }
  109. }
  110. protected override void InternalUpdateQueryParameters()
  111. {
  112. try
  113. {
  114. queryParameters.Add("uploadId", uploadId);
  115. }
  116. catch (ArgumentException)
  117. {
  118. queryParameters["uploadId"] = uploadId;
  119. }
  120. try
  121. {
  122. queryParameters.Add("partNumber", partNumber.ToString());
  123. }
  124. catch (ArgumentException)
  125. {
  126. queryParameters["partNumber"] = partNumber.ToString();
  127. }
  128. }
  129. public override Network.RequestBody GetRequestBody()
  130. {
  131. RequestBody body = null;
  132. if (srcPath != null)
  133. {
  134. FileInfo fileInfo = new FileInfo(srcPath);
  135. if (contentLength == -1 || contentLength + fileOffset > fileInfo.Length)
  136. {
  137. contentLength = fileInfo.Length - fileOffset;
  138. }
  139. body = new FileRequestBody(srcPath, fileOffset, contentLength);
  140. body.ProgressCallback = progressCallback;
  141. }
  142. return body;
  143. }
  144. }
  145. }