DownLoadCOSXRItem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using COSXML;
  7. using COSXML.Auth;
  8. using COSXML.Model;
  9. using COSXML.Model.Object;
  10. using COSXML.Transfer;
  11. using LitJson;
  12. using Newtonsoft.Json;
  13. using Newtonsoft.Json.Linq;
  14. using UnityEngine;
  15. using UnityEngine.Networking;
  16. using XRTool.Util;
  17. using static DownLoadXRManager;
  18. public class DownLoadCOSXRItem : MonoBehaviour
  19. {
  20. public DownLoadConfig cosconifg;
  21. public PutFileData uploadconifg;
  22. public static CosXmlServer materialCosXml;
  23. public static Dictionary<string, List<DownLoadConfig>> downLoadingList = new Dictionary<string, List<DownLoadConfig>>();
  24. public void startDownload(DownLoadConfig conifg)
  25. {
  26. cosconifg = conifg;
  27. InitMaterialCos();
  28. StartCoroutine(ReadStreamingAssetsFile());
  29. }
  30. public void startUpLoad(PutFileData conifg)
  31. {
  32. uploadconifg = conifg;
  33. InitMaterialCos();
  34. StartCoroutine(ReadStreamingUpLoad());
  35. }
  36. private void InitMaterialCos()
  37. {
  38. CosXmlConfig config = new CosXmlConfig.Builder()
  39. .IsHttps(true)
  40. .SetRegion(cospingzheng.region)
  41. .SetDebugLog(true)
  42. .Build();
  43. Debug.Log("ReadStreamingAssetsFile==SetRegion=>" + cospingzheng.region);
  44. QCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(
  45. cospingzheng.credentials.tmpSecretId, cospingzheng.credentials.tmpSecretKey, cospingzheng.expiredTime, cospingzheng.credentials.token);
  46. Debug.Log("ReadStreamingAssetsFile==tmpSecretId=>" + cospingzheng.credentials.tmpSecretId +
  47. " tmpSecretKey==>" + cospingzheng.credentials.tmpSecretKey +
  48. " expiredTime==>" + cospingzheng.expiredTime +
  49. " sessionToken==>" + cospingzheng.credentials.token);
  50. materialCosXml = new CosXmlServer(config, cosCredentialProvider);
  51. }
  52. IEnumerator ReadStreamingUpLoad()
  53. {
  54. if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR/"))
  55. {
  56. Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR/");
  57. }
  58. //.cssg-snippet-body-start:[transfer-batch-download-objects]
  59. TransferConfig transferConfig = new TransferConfig();
  60. // 初始化 TransferManager
  61. TransferManager transferManager = new TransferManager(materialCosXml, transferConfig);
  62. // String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键
  63. // string localDir = System.IO.Path.GetTempPath();//本地文件夹
  64. // string localFileName = "my-local-temp-file"; //指定本地保存的文件名
  65. Debug.Log("ReadStreamingAssetsFile==cospingzheng.bucket=>" + cospingzheng.bucket+
  66. " uploadconifg.objectName==>" + uploadconifg.objectName +
  67. " uploadconifg.filePath==>" + uploadconifg.filePath) ;
  68. PutObjectRequest putrequest = new PutObjectRequest(cospingzheng.bucket, uploadconifg.objectName, uploadconifg.filePath);
  69. Dictionary<string, string> dicheaders = new Dictionary<string, string>();
  70. dicheaders.Add("X-Amz-Meta-Uuid", uploadconifg.uuid);
  71. putrequest.SetRequestHeaders(dicheaders);
  72. // 下载对象
  73. COSXMLUploadTask UpLoadTask = new COSXMLUploadTask(putrequest);
  74. UpLoadTask.SetSrcPath(uploadconifg.filePath);
  75. // 手动设置高级下载接口的并发数 (默认为5), 从5.4.26版本开始支持!
  76. //downloadTask.SetMaxTasks(10);
  77. UpLoadTask.progressCallback = delegate (long completed, long total)
  78. {
  79. // uploadconifg.presson?.Invoke(completed * 100.0f / total);
  80. Debug.Log(String.Format("Hjj UpLoad" + uploadconifg.filePath + "progress = {0:##.##}%", completed * 100.0 / total));
  81. //showDownLoadPre = completed * 100.0f / total;
  82. };
  83. // Debug.Log(downloadMaterial.downLoadPath + "||||||||| DGJ");
  84. UpLoadTask.onState += OnStateUpLoadTask;
  85. UpLoadTask.successCallback += SuccessCallback;
  86. // listDownLoadTask.Add(downloadTask);
  87. Task<COSXMLUploadTask.UploadTaskResult> task = null;
  88. try
  89. {
  90. task = transferManager.UploadAsync(UpLoadTask);
  91. }
  92. catch (COSXML.CosException.CosClientException clientEx)
  93. {
  94. Debug.Log("CosClientException===>" + clientEx);
  95. //请求失败
  96. }
  97. catch (COSXML.CosException.CosServerException serverEx)
  98. {
  99. Debug.Log("CosClientException===>" + serverEx);
  100. //请求失败
  101. }
  102. yield return task;
  103. while (!task.IsCompleted)
  104. {
  105. yield return null;
  106. }
  107. /*
  108. Debug.Log("ReadStreamingAssetsFile==IsCompleted=>" + task.IsCompleted);
  109. Debug.Log("ReadStreamingAssetsFile===>" + task.IsFaulted);
  110. Debug.Log("ReadStreamingAssetsFile===>" + task.IsCompletedSuccessfully);
  111. Debug.Log("ReadStreamingAssetsFile===>" + task.Status);
  112. Debug.Log("ReadStreamingAssetsFile===>" + task.Exception);*/
  113. // Debug.Log("ReadStreamingAssetsFile===>" + task.Result.RawContentBodyString);
  114. // Destroy(this.gameObject);
  115. }
  116. public string localPath;
  117. IEnumerator ReadStreamingAssetsFile()
  118. {
  119. if(!Directory.Exists(Application.persistentDataPath + "/DownLoadXR/"))
  120. {
  121. Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR/");
  122. }
  123. //.cssg-snippet-body-start:[transfer-batch-download-objects]
  124. TransferConfig transferConfig = new TransferConfig();
  125. // 初始化 TransferManager
  126. TransferManager transferManager = new TransferManager(materialCosXml, transferConfig);
  127. // String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键
  128. // string localDir = System.IO.Path.GetTempPath();//本地文件夹
  129. // string localFileName = "my-local-temp-file"; //指定本地保存的文件名
  130. localPath = Application.persistentDataPath + "/DownLoadXR/" + cosconifg.data.path.Split("/")[cosconifg.data.path.Split("/").Length - 1];
  131. string localFileName = Path.GetFileName(localPath);
  132. Debug.Log("ReadStreamingAssetsFile==cospingzheng.bucket=>" + cospingzheng.bucket +
  133. " cosconifg.data.path==>" + cosconifg.data.path +
  134. " localPathh==>" + localPath +
  135. " localFileName==>" + localFileName);
  136. // 下载对象
  137. COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(cospingzheng.bucket, cosconifg.data.path,
  138. Application.persistentDataPath + "/DownLoadXR/", localFileName);
  139. // 手动设置高级下载接口的并发数 (默认为5), 从5.4.26版本开始支持!
  140. //downloadTask.SetMaxTasks(10);
  141. downloadTask.progressCallback = delegate (long completed, long total)
  142. {
  143. cosconifg.presson?.Invoke(completed * 100.0f / total);
  144. Debug.Log(String.Format("Hjj download"+ cosconifg.data.path + "progress = {0:##.##}%", completed * 100.0 / total));
  145. //showDownLoadPre = completed * 100.0f / total;
  146. };
  147. // Debug.Log(downloadMaterial.downLoadPath + "||||||||| DGJ");
  148. downloadTask.onState += OnState;
  149. downloadTask.successCallback += SuccessCallback;
  150. // listDownLoadTask.Add(downloadTask);
  151. Task<COSXMLDownloadTask.DownloadTaskResult> task = null;
  152. try
  153. {
  154. task = transferManager.DownloadAsync(downloadTask);
  155. }
  156. catch (COSXML.CosException.CosClientException clientEx)
  157. {
  158. Debug.Log("CosClientException===>" + clientEx);
  159. //请求失败
  160. }
  161. catch (COSXML.CosException.CosServerException serverEx)
  162. {
  163. Debug.Log("CosClientException===>" + serverEx);
  164. //请求失败
  165. }
  166. yield return task;
  167. while(!task.IsCompleted)
  168. {
  169. yield return null;
  170. }
  171. Debug.Log("ReadStreamingAssetsFile==IsCompleted=>" + task.IsCompleted);
  172. Debug.Log("ReadStreamingAssetsFile===>" + task.IsFaulted);
  173. Debug.Log("ReadStreamingAssetsFile===>" + task.IsCompletedSuccessfully);
  174. Debug.Log("ReadStreamingAssetsFile===>" + task.Status);
  175. Debug.Log("ReadStreamingAssetsFile===>" + task.Exception);
  176. Debug.Log("ReadStreamingAssetsFile===>" + task.Result.RawContentBodyString);
  177. // Destroy(this.gameObject);
  178. }
  179. private void OnStateUpLoadTask(TaskState taskState)
  180. {
  181. taskUploadQueue.Enqueue(taskState);
  182. }
  183. private void SuccessCallback(CosResult cosResult)
  184. {
  185. if(cosResult.IsSuccessful())
  186. {
  187. Debug.Log("SuccessCallback===>" + cosResult.httpMessage);
  188. Debug.Log("SuccessCallback===>" + cosResult.httpCode);
  189. Debug.Log("SuccessCallback===>" + cosResult.Key);
  190. }
  191. }
  192. public Queue<TaskState> taskQueue = new Queue<TaskState>();
  193. public Queue<TaskState> taskUploadQueue = new Queue<TaskState>();
  194. private void OnState(TaskState state)
  195. {
  196. taskQueue.Enqueue(state);
  197. }
  198. public byte[] readbytes(Stream stream)
  199. {
  200. using (Stream fs = stream)
  201. {
  202. byte[] bytes = new byte[fs.Length];
  203. fs.Read(bytes, 0, bytes.Length);
  204. fs.Close();
  205. return bytes;
  206. }
  207. }
  208. private void Update()
  209. {
  210. if (taskQueue.Count > 0)
  211. {
  212. var state = taskQueue.Dequeue();
  213. if (state == TaskState.Completed || state == TaskState.Failed)
  214. {
  215. if (state == TaskState.Completed)
  216. {
  217. FileInfo file = new FileInfo(localPath);
  218. cosconifg.bytes(readbytes(file.OpenRead()));
  219. }
  220. Debug.Log("Hjj download" + cosconifg.data.path + "TaskState.=>" + state);
  221. }
  222. }
  223. if (taskUploadQueue.Count > 0)
  224. {
  225. var state = taskUploadQueue.Dequeue();
  226. if (state == TaskState.Completed || state == TaskState.Failed)
  227. {
  228. uploadconifg.callBack?.Invoke(state.ToString());
  229. }
  230. }
  231. }
  232. }