LangChaoRommMinIo.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. StartCoroutine(HttpToolLangChao.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. Debug.Log(minio == null);
  148. TimerMgr.Instance.CreateTimer(() =>
  149. {
  150. minio = null;
  151. }, 3000);
  152. callBack?.Invoke(true);
  153. }
  154. catch
  155. {
  156. callBack?.Invoke(false);
  157. }
  158. }));
  159. }
  160. Queue<GetFileData> fdQueue = new Queue<GetFileData>();
  161. public void getFile(string roomId, string bucket, string objectName, Action<RoomFileData> callBack)//, string path
  162. {
  163. if (this.projectId!= roomId)
  164. {
  165. this.projectId = roomId;
  166. // minio = null;
  167. }
  168. //Unity 当执行大量null 引用操作的时候 下面的方法比 gameobject!=null 快了大概两倍
  169. if (!System.Object.ReferenceEquals(gameObject, null))
  170. {
  171. }
  172. GetFileData getFileData = new GetFileData();
  173. getFileData.bucket = bucket;
  174. getFileData.objectName = objectName;
  175. getFileData.callBack = callBack;
  176. fdQueue.Enqueue(getFileData);
  177. }
  178. public class RoomFileData
  179. {
  180. public byte[] bytes;
  181. public string url;
  182. }
  183. public class RoomFileDataBack
  184. {
  185. public RoomFileData rfd;
  186. public GetFileData Nowfd;
  187. }
  188. GetFileData Nowfd;
  189. private void Update()
  190. {
  191. if (fdQueue.Count > 0 && Nowfd == null)
  192. {
  193. Nowfd = fdQueue.Dequeue();
  194. }
  195. if (minio == null)
  196. {
  197. if (projectId !=null && !isminioUpdate)
  198. {
  199. isminioUpdate = true;
  200. initMinIo((bool b) =>
  201. {
  202. isminioUpdate = false;
  203. });
  204. }
  205. }
  206. if (backQueue.Count > 0 )
  207. {
  208. RoomFileDataBack rfdb = backQueue.Dequeue();
  209. try
  210. {
  211. rfdb.Nowfd.callBack?.Invoke(rfdb.rfd);
  212. }
  213. catch
  214. {
  215. }
  216. isUpdate = false;
  217. Nowfd = null;
  218. }
  219. }
  220. public class GetFileData
  221. {
  222. public string bucket;
  223. public string objectName;
  224. public Action<RoomFileData> callBack;
  225. }
  226. }