LangChaoRommMinIo.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using LitJson;
  2. using Minio;
  3. using Minio.DataModel.Args;
  4. using Minio.Exceptions;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using UnityEngine;
  12. using UnityEngine.Networking;
  13. using XRTool.Util;
  14. public class LangChaoRommMinIo : MonoSingleton<LangChaoRommMinIo>
  15. {
  16. IMinioClient minio;
  17. string path;
  18. string bucket;
  19. string host;
  20. string tmpSecretId;
  21. string tmpSecretKey;
  22. string minioToken;
  23. string projectId;
  24. string region;
  25. Thread thread;
  26. private void Start()
  27. {
  28. thread = new Thread(DoSomeWork);
  29. thread.Start();
  30. }
  31. bool isminioUpdate;
  32. bool isUpdate;
  33. void DoSomeWork()
  34. {
  35. while (true)
  36. {
  37. if (minio == null)
  38. {
  39. }
  40. else
  41. {
  42. if (Nowfd != null && !isUpdate)
  43. {
  44. isUpdate = true;
  45. Run().Wait();
  46. }
  47. }
  48. Thread.Sleep(1000);
  49. }
  50. }
  51. public static byte[] StreamToBytes(Stream stream)
  52. {
  53. byte[] bytes = new byte[stream.Length];
  54. stream.Read(bytes, 0, bytes.Length);
  55. stream.Seek(0, SeekOrigin.Begin);
  56. return bytes;
  57. }
  58. Queue<RoomFileDataBack> backQueue = new Queue<RoomFileDataBack>();
  59. // File uploader task.
  60. private async Task Run()
  61. {
  62. /*
  63. StatObjectArgs statObjectArgs = new StatObjectArgs()
  64. .WithBucket(Nowfd.bucket)
  65. .WithObject(Nowfd.objectName);
  66. await minio.StatObjectAsync(statObjectArgs);
  67. PresignedGetObjectArgs args = new PresignedGetObjectArgs()
  68. .WithBucket(Nowfd.bucket)
  69. .WithObject(Nowfd.objectName)
  70. .WithExpiry(60 * 60 * 24);
  71. string url = await minio.PresignedGetObjectAsync(args);
  72. RoomFileData rfd = new RoomFileData();
  73. rfd.url = url;
  74. RoomFileDataBack rfdb = new RoomFileDataBack();
  75. rfdb.rfd = rfd;
  76. rfdb.Nowfd = Nowfd;
  77. backQueue.Enqueue(rfdb);*/
  78. StatObjectArgs statObjectArgs = new StatObjectArgs()
  79. .WithBucket(Nowfd.bucket)
  80. .WithObject(Nowfd.objectName);
  81. //.WithBucket("cmcc")
  82. //.WithObject("remote/845995/eb00bda9-c63b-4b93-a801-e45548e7a8c2.pdf");
  83. await minio.StatObjectAsync(statObjectArgs);
  84. // Get input stream to have content of 'my-objectname' from 'my-bucketname'
  85. GetObjectArgs getObjectArgs = new GetObjectArgs()
  86. .WithBucket(Nowfd.bucket)
  87. .WithObject(Nowfd.objectName)
  88. .WithCallbackStream(async (stream) =>
  89. {
  90. PresignedGetObjectArgs args = new PresignedGetObjectArgs()
  91. .WithBucket(Nowfd.bucket)
  92. .WithObject(Nowfd.objectName)
  93. .WithExpiry(60 * 60 * 24);
  94. string url = await minio.PresignedGetObjectAsync(args);
  95. RoomFileData rfd = new RoomFileData();
  96. rfd.url = url;
  97. rfd.bytes = StreamToBytes(stream);
  98. RoomFileDataBack rfdb = new RoomFileDataBack();
  99. rfdb.rfd = rfd;
  100. rfdb.Nowfd = Nowfd;
  101. backQueue.Enqueue(rfdb);
  102. rfd.url = Application.persistentDataPath + "/" + Path.GetFileName(url);
  103. });
  104. await minio.GetObjectAsync(getObjectArgs);
  105. }
  106. void initMinIo(Action<bool> callBack)
  107. {
  108. JsonData data = new JsonData();
  109. data["roomId"] = projectId;
  110. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.storage_roomCredential, data.ToJson(), async (string str) =>
  111. {
  112. try
  113. {
  114. JsonData d = JsonMapper.ToObject(str);
  115. minioToken = d["data"]["credentials"]["token"].ToString();
  116. tmpSecretId = d["data"]["credentials"]["tmpSecretId"].ToString();
  117. tmpSecretKey = d["data"]["credentials"]["tmpSecretKey"].ToString();
  118. host = d["data"]["host"].ToString();
  119. bucket = d["data"]["bucket"].ToString();
  120. path = d["data"]["path"].ToString();
  121. Debug.Log(str);
  122. bool isHttps = host.Contains("https");
  123. var endpoint = host.Split("//")[1];
  124. var accessKey = tmpSecretId;//"tr6Nh5D8bnlGaLJE6vb5";
  125. var secretKey = tmpSecretKey;// "aVOYdXLnX4MCiKbit8aomZNWvAx8YSpzhiwzFhrI";
  126. Debug.Log("endpoint===>" + endpoint + " isHttps==>" + isHttps);
  127. if(d["data"]["region"]!=null&& !string.IsNullOrEmpty(d["data"]["region"].ToString())&& d["data"]["region"].ToString()!="null")
  128. {
  129. region = d["data"]["region"].ToString();
  130. minio = new MinioClient()
  131. .WithEndpoint(endpoint)
  132. .WithCredentials(accessKey, secretKey)
  133. .WithSessionToken(minioToken)
  134. .WithSSL(isHttps)
  135. .WithRegion(region)
  136. .Build();
  137. }
  138. else
  139. {
  140. minio = new MinioClient()
  141. .WithEndpoint(endpoint)
  142. .WithCredentials(accessKey, secretKey)
  143. .WithSessionToken(minioToken)
  144. .WithSSL(isHttps)
  145. .Build();
  146. }
  147. TimerMgr.Instance.CreateTimer(() =>
  148. {
  149. minio = null;
  150. }, 3000);
  151. callBack?.Invoke(true);
  152. }
  153. catch
  154. {
  155. callBack?.Invoke(false);
  156. }
  157. }));
  158. }
  159. Queue<GetFileData> fdQueue = new Queue<GetFileData>();
  160. public void getFile(string roomId, string bucket, string objectName, Action<RoomFileData> callBack)//, string path
  161. {
  162. if (this.projectId!= roomId)
  163. {
  164. this.projectId = roomId;
  165. // minio = null;
  166. }
  167. //Unity 当执行大量null 引用操作的时候 下面的方法比 gameobject!=null 快了大概两倍
  168. if (!System.Object.ReferenceEquals(gameObject, null))
  169. {
  170. }
  171. GetFileData getFileData = new GetFileData();
  172. getFileData.bucket = bucket;
  173. getFileData.objectName = objectName;
  174. getFileData.callBack = callBack;
  175. fdQueue.Enqueue(getFileData);
  176. }
  177. public class RoomFileData
  178. {
  179. public byte[] bytes;
  180. public string url;
  181. }
  182. public class RoomFileDataBack
  183. {
  184. public RoomFileData rfd;
  185. public GetFileData Nowfd;
  186. }
  187. GetFileData Nowfd;
  188. private void Update()
  189. {
  190. if (fdQueue.Count > 0 && Nowfd == null)
  191. {
  192. Nowfd = fdQueue.Dequeue();
  193. }
  194. if (minio == null)
  195. {
  196. if (projectId !=null && !isminioUpdate)
  197. {
  198. isminioUpdate = true;
  199. initMinIo((bool b) =>
  200. {
  201. isminioUpdate = false;
  202. });
  203. }
  204. }
  205. if (backQueue.Count > 0 )
  206. {
  207. RoomFileDataBack rfdb = backQueue.Dequeue();
  208. try
  209. {
  210. rfdb.Nowfd.callBack?.Invoke(rfdb.rfd);
  211. }
  212. catch
  213. {
  214. }
  215. isUpdate = false;
  216. Nowfd = null;
  217. }
  218. }
  219. public class GetFileData
  220. {
  221. public string bucket;
  222. public string objectName;
  223. public Action<RoomFileData> callBack;
  224. }
  225. }