LangChaoRommMinIo.cs 8.5 KB

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