TransferManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using COSXML.Model;
  7. using COSXML.CosException;
  8. namespace COSXML.Transfer
  9. {
  10. /// <summary>
  11. /// 高级传输,提供更方便的对象上传、下载、拷贝功能
  12. /// </summary>
  13. public sealed class TransferManager
  14. {
  15. private TransferConfig transferConfig;
  16. private CosXml cosXml;
  17. /// <summary>
  18. /// 初始化方法
  19. /// </summary>
  20. /// <param name="cosXmlServer">COSXML服务</param>
  21. /// <param name="transferConfig">高级传输设置</param>
  22. public TransferManager(CosXml cosXmlServer, TransferConfig transferConfig)
  23. {
  24. if (cosXmlServer == null)
  25. {
  26. // throw new ArgumentNullException("CosXmlServer = null");
  27. }
  28. if (transferConfig == null)
  29. {
  30. // throw new ArgumentNullException("TransferConfig = null");
  31. }
  32. this.transferConfig = transferConfig;
  33. //COSXMLTask.InitCosXmlServer(cosXmlServer);
  34. this.cosXml = cosXmlServer;
  35. }
  36. /// <summary>
  37. /// 上传对象,封装了简单上传、分片上传功能。
  38. /// </summary>
  39. /// <param name="uploader"></param>
  40. [Obsolete("方法已废弃,请使用 UploadAsync 方法实现异步调用。")]
  41. public void Upload(COSXMLUploadTask uploader)
  42. {
  43. uploader.InitCosXmlServer(cosXml);
  44. uploader.SetDivision(transferConfig.DivisionForUpload, transferConfig.SliceSizeForUpload);
  45. uploader.Upload();
  46. }
  47. /// <summary>
  48. /// 异步上传对象,封装了简单上传、分片上传功能。
  49. /// </summary>
  50. /// <param name="uploader"></param>
  51. /// <returns></returns>
  52. public Task<COSXMLUploadTask.UploadTaskResult> UploadAsync(COSXMLUploadTask uploader)
  53. {
  54. var task = uploader.AsyncTask<COSXMLUploadTask.UploadTaskResult>();
  55. Upload(uploader);
  56. return task;
  57. }
  58. /// <summary>
  59. /// 下载对象
  60. /// </summary>
  61. /// <param name="downloader"></param>
  62. [Obsolete("方法已废弃,请使用 DownloadAsync 方法实现异步调用。")]
  63. public void Download(COSXMLDownloadTask downloader)
  64. {
  65. downloader.InitCosXmlServer(cosXml);
  66. downloader.SetSliceSize(transferConfig.SliceSizeForDownload);
  67. downloader.SetDivisionSize(transferConfig.DivisionForDownload);
  68. downloader.Download();
  69. }
  70. /// <summary>
  71. /// 异步下载对象
  72. /// </summary>
  73. /// <param name="downloader"></param>
  74. /// <returns></returns>
  75. public Task<COSXMLDownloadTask.DownloadTaskResult> DownloadAsync(COSXMLDownloadTask downloader)
  76. {
  77. var task = downloader.AsyncTask<COSXMLDownloadTask.DownloadTaskResult>();
  78. Download(downloader);
  79. return task;
  80. }
  81. /// <summary>
  82. /// 拷贝对象,封装了简单拷贝、分片拷贝功能。
  83. /// </summary>
  84. /// <param name="copy"></param>
  85. [Obsolete("方法已废弃,请使用 CopyAsync 方法实现异步调用。")]
  86. public void Copy(COSXMLCopyTask copy)
  87. {
  88. copy.InitCosXmlServer(cosXml);
  89. copy.SetDivision(transferConfig.DdivisionForCopy, transferConfig.SliceSizeForCopy);
  90. copy.Copy();
  91. }
  92. /// <summary>
  93. /// 异步拷贝对象,封装了简单拷贝、分片拷贝功能。
  94. /// </summary>
  95. /// <param name="copyTask"></param>
  96. /// <returns></returns>
  97. public Task<COSXMLCopyTask.CopyTaskResult> CopyAsync(COSXMLCopyTask copyTask)
  98. {
  99. var task = copyTask.AsyncTask<COSXMLCopyTask.CopyTaskResult>();
  100. Copy(copyTask);
  101. return task;
  102. }
  103. }
  104. /// <summary>
  105. /// 高级传输任务设置
  106. /// </summary>
  107. public sealed class TransferConfig
  108. {
  109. // 5M
  110. private long divisionForCopy = 5242880;
  111. // 2M
  112. private long sliceSizeForCopy = 2097152;
  113. // 5M
  114. private long divisionForUpload = 5242880;
  115. // 1M
  116. private long sliceSizeForUpload = 1048576;
  117. // 20M
  118. private long divisionForDownload = 20971520;
  119. // 10M
  120. private long sliceSizeForDownload = 10485760;
  121. /// <summary>
  122. /// 多大的文件会自动使用分片拷贝
  123. /// </summary>
  124. /// <value>默认是 5MB</value>
  125. public long DdivisionForCopy
  126. {
  127. get
  128. {
  129. return divisionForCopy;
  130. }
  131. set { divisionForCopy = value; }
  132. }
  133. /// <summary>
  134. /// 多大的文件会自动使用分片上传
  135. /// </summary>
  136. /// <value>默认是 2MB</value>
  137. public long DivisionForUpload
  138. {
  139. get
  140. {
  141. return divisionForUpload;
  142. }
  143. set { divisionForUpload = value; }
  144. }
  145. /// <summary>
  146. /// 每个分片拷贝任务的分片大小
  147. /// </summary>
  148. /// <value>默认是 5MB</value>
  149. public long SliceSizeForCopy
  150. {
  151. get
  152. {
  153. return sliceSizeForCopy;
  154. }
  155. set { sliceSizeForCopy = value; }
  156. }
  157. /// <summary>
  158. /// 每个分片上传任务的分片大小
  159. /// </summary>
  160. /// <value>默认是 1MB</value>
  161. public long SliceSizeForUpload
  162. {
  163. get
  164. {
  165. return sliceSizeForUpload;
  166. }
  167. set { sliceSizeForUpload = value; }
  168. }
  169. /// <summary>
  170. /// 多大的文件会使用多线程分块下载
  171. /// </summary>
  172. /// <value>默认是 20MB</value>
  173. public long DivisionForDownload
  174. {
  175. get
  176. {
  177. return divisionForDownload;
  178. }
  179. set { divisionForDownload = value; }
  180. }
  181. /// <summary>
  182. /// 分块下载的每个块大小
  183. /// </summary>
  184. /// <value>默认是 10MB</value>
  185. public long SliceSizeForDownload
  186. {
  187. get
  188. {
  189. return sliceSizeForDownload;
  190. }
  191. set { sliceSizeForDownload = value; }
  192. }
  193. }
  194. }