GetObjectRequest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Common;
  5. using COSXML.CosException;
  6. using System.IO;
  7. namespace COSXML.Model.Object
  8. {
  9. /// <summary>
  10. /// 下载对象
  11. /// <see href="https://cloud.tencent.com/document/product/436/7753"/>
  12. /// </summary>
  13. public sealed class GetObjectRequest : ObjectRequest
  14. {
  15. /// <summary>
  16. /// 保存文件的本地文件夹路径
  17. /// </summary>
  18. public string localDir;
  19. /// <summary>
  20. /// 保存文件的本地的文件名
  21. /// </summary>
  22. public string localFileName;
  23. /// <summary>
  24. /// 保存文件的本地偏移位置,下载的数据从此处开始append该文件后面
  25. /// </summary>
  26. private long localFileOffset = 0;
  27. /// <summary>
  28. /// 下载进度回调
  29. /// </summary>
  30. private COSXML.Callback.OnProgressCallback progressCallback;
  31. public GetObjectRequest(string bucket, string key, string localDir, string localFileName)
  32. : base(bucket, key)
  33. {
  34. this.method = CosRequestMethod.GET;
  35. this.localDir = localDir;
  36. this.localFileName = localFileName;
  37. }
  38. /// <summary>
  39. /// 下载进度回调
  40. /// </summary>
  41. /// <param name="progressCallback"></param>
  42. public void SetCosProgressCallback(COSXML.Callback.OnProgressCallback progressCallback)
  43. {
  44. this.progressCallback = progressCallback;
  45. }
  46. internal COSXML.Callback.OnProgressCallback GetCosProgressCallback()
  47. {
  48. return progressCallback;
  49. }
  50. /// <summary>
  51. /// 保存文件的本地偏移位置,下载的数据从此处开始append该文件后面
  52. /// </summary>
  53. /// <param name="localFileOffset"></param>
  54. public void SetLocalFileOffset(long localFileOffset)
  55. {
  56. this.localFileOffset = localFileOffset > 0 ? localFileOffset : 0;
  57. }
  58. public long GetLocalFileOffset()
  59. {
  60. return localFileOffset;
  61. }
  62. /// <summary>
  63. /// 下载内容范围
  64. /// </summary>
  65. /// <param name="start"></param>
  66. /// <param name="end"></param>
  67. public void SetRange(long start, long end)
  68. {
  69. if (start < 0)
  70. {
  71. return;
  72. }
  73. if (end < start)
  74. {
  75. end = -1;
  76. }
  77. SetRequestHeader(CosRequestHeaderKey.RANGE, String.Format("bytes={0}-{1}", start,
  78. (end == -1 ? "" : end.ToString())));
  79. }
  80. /// <summary>
  81. /// 下载内容的起始偏移量
  82. /// </summary>
  83. /// <param name="start"></param>
  84. public void SetRange(long start)
  85. {
  86. SetRange(start, -1);
  87. }
  88. /// <summary>
  89. /// 最大下载速度,单位是 bit/s
  90. /// </summary>
  91. /// <param name="rate"></param>
  92. public void LimitTraffic(long rate)
  93. {
  94. SetRequestHeader(CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT, rate.ToString());
  95. }
  96. /// <summary>
  97. /// 下载特定版本的对象
  98. /// </summary>
  99. /// <param name="versionId"></param>
  100. public void SetVersionId(string versionId)
  101. {
  102. if (versionId != null)
  103. {
  104. SetQueryParameter(CosRequestHeaderKey.VERSION_ID, versionId);
  105. }
  106. }
  107. public override void CheckParameters()
  108. {
  109. if (localDir == null)
  110. {
  111. throw new CosClientException((int)CosClientError.InvalidArgument, "localDir = null");
  112. }
  113. if (requestUrlWithSign != null && localFileName == null)
  114. {
  115. throw new CosClientException((int)CosClientError.InvalidArgument, "localFileName = null");
  116. }
  117. base.CheckParameters();
  118. }
  119. /// <summary>
  120. /// 获取本地文件保存路径
  121. /// </summary>
  122. /// <returns></returns>
  123. public string GetSaveFilePath()
  124. {
  125. string result = localDir;
  126. DirectoryInfo dirInfo = new DirectoryInfo(localDir);
  127. if (!dirInfo.Exists)
  128. {
  129. dirInfo.Create();
  130. }
  131. if (String.IsNullOrEmpty(localFileName))
  132. {
  133. localFileName = path;
  134. }
  135. if (localDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString(), StringComparison.OrdinalIgnoreCase))
  136. {
  137. result = result + localFileName;
  138. }
  139. else
  140. {
  141. result = result + System.IO.Path.DirectorySeparatorChar + localFileName;
  142. }
  143. return result;
  144. }
  145. }
  146. }