123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using COSXML.Model;
- using COSXML.CosException;
- namespace COSXML.Transfer
- {
- /// <summary>
- /// 高级传输,提供更方便的对象上传、下载、拷贝功能
- /// </summary>
- public sealed class TransferManager
- {
- private TransferConfig transferConfig;
- private CosXml cosXml;
- /// <summary>
- /// 初始化方法
- /// </summary>
- /// <param name="cosXmlServer">COSXML服务</param>
- /// <param name="transferConfig">高级传输设置</param>
- public TransferManager(CosXml cosXmlServer, TransferConfig transferConfig)
- {
- if (cosXmlServer == null)
- {
- // throw new ArgumentNullException("CosXmlServer = null");
- }
- if (transferConfig == null)
- {
- // throw new ArgumentNullException("TransferConfig = null");
- }
- this.transferConfig = transferConfig;
- //COSXMLTask.InitCosXmlServer(cosXmlServer);
- this.cosXml = cosXmlServer;
- }
- /// <summary>
- /// 上传对象,封装了简单上传、分片上传功能。
- /// </summary>
- /// <param name="uploader"></param>
- [Obsolete("方法已废弃,请使用 UploadAsync 方法实现异步调用。")]
- public void Upload(COSXMLUploadTask uploader)
- {
- uploader.InitCosXmlServer(cosXml);
- uploader.SetDivision(transferConfig.DivisionForUpload, transferConfig.SliceSizeForUpload);
- uploader.Upload();
- }
- /// <summary>
- /// 异步上传对象,封装了简单上传、分片上传功能。
- /// </summary>
- /// <param name="uploader"></param>
- /// <returns></returns>
- public Task<COSXMLUploadTask.UploadTaskResult> UploadAsync(COSXMLUploadTask uploader)
- {
- var task = uploader.AsyncTask<COSXMLUploadTask.UploadTaskResult>();
- Upload(uploader);
- return task;
- }
- /// <summary>
- /// 下载对象
- /// </summary>
- /// <param name="downloader"></param>
- [Obsolete("方法已废弃,请使用 DownloadAsync 方法实现异步调用。")]
- public void Download(COSXMLDownloadTask downloader)
- {
- downloader.InitCosXmlServer(cosXml);
- downloader.SetSliceSize(transferConfig.SliceSizeForDownload);
- downloader.SetDivisionSize(transferConfig.DivisionForDownload);
- downloader.Download();
- }
- /// <summary>
- /// 异步下载对象
- /// </summary>
- /// <param name="downloader"></param>
- /// <returns></returns>
- public Task<COSXMLDownloadTask.DownloadTaskResult> DownloadAsync(COSXMLDownloadTask downloader)
- {
- var task = downloader.AsyncTask<COSXMLDownloadTask.DownloadTaskResult>();
- Download(downloader);
- return task;
- }
- /// <summary>
- /// 拷贝对象,封装了简单拷贝、分片拷贝功能。
- /// </summary>
- /// <param name="copy"></param>
- [Obsolete("方法已废弃,请使用 CopyAsync 方法实现异步调用。")]
- public void Copy(COSXMLCopyTask copy)
- {
- copy.InitCosXmlServer(cosXml);
- copy.SetDivision(transferConfig.DdivisionForCopy, transferConfig.SliceSizeForCopy);
- copy.Copy();
- }
- /// <summary>
- /// 异步拷贝对象,封装了简单拷贝、分片拷贝功能。
- /// </summary>
- /// <param name="copyTask"></param>
- /// <returns></returns>
- public Task<COSXMLCopyTask.CopyTaskResult> CopyAsync(COSXMLCopyTask copyTask)
- {
- var task = copyTask.AsyncTask<COSXMLCopyTask.CopyTaskResult>();
- Copy(copyTask);
- return task;
- }
- }
- /// <summary>
- /// 高级传输任务设置
- /// </summary>
- public sealed class TransferConfig
- {
- // 5M
- private long divisionForCopy = 5242880;
- // 2M
- private long sliceSizeForCopy = 2097152;
- // 5M
- private long divisionForUpload = 5242880;
- // 1M
- private long sliceSizeForUpload = 1048576;
- // 20M
- private long divisionForDownload = 20971520;
- // 10M
- private long sliceSizeForDownload = 10485760;
- /// <summary>
- /// 多大的文件会自动使用分片拷贝
- /// </summary>
- /// <value>默认是 5MB</value>
- public long DdivisionForCopy
- {
- get
- {
- return divisionForCopy;
- }
- set { divisionForCopy = value; }
- }
- /// <summary>
- /// 多大的文件会自动使用分片上传
- /// </summary>
- /// <value>默认是 2MB</value>
- public long DivisionForUpload
- {
- get
- {
- return divisionForUpload;
- }
- set { divisionForUpload = value; }
- }
- /// <summary>
- /// 每个分片拷贝任务的分片大小
- /// </summary>
- /// <value>默认是 5MB</value>
- public long SliceSizeForCopy
- {
- get
- {
- return sliceSizeForCopy;
- }
- set { sliceSizeForCopy = value; }
- }
- /// <summary>
- /// 每个分片上传任务的分片大小
- /// </summary>
- /// <value>默认是 1MB</value>
- public long SliceSizeForUpload
- {
- get
- {
- return sliceSizeForUpload;
- }
- set { sliceSizeForUpload = value; }
- }
- /// <summary>
- /// 多大的文件会使用多线程分块下载
- /// </summary>
- /// <value>默认是 20MB</value>
- public long DivisionForDownload
- {
- get
- {
- return divisionForDownload;
- }
- set { divisionForDownload = value; }
- }
- /// <summary>
- /// 分块下载的每个块大小
- /// </summary>
- /// <value>默认是 10MB</value>
- public long SliceSizeForDownload
- {
- get
- {
- return sliceSizeForDownload;
- }
- set { sliceSizeForDownload = value; }
- }
- }
- }
|