LangChaoRommMinIo.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. StatObjectArgs statObjectArgs = new StatObjectArgs()
  62. .WithBucket(Nowfd.bucket)
  63. .WithObject(Nowfd.objectName);
  64. await minio.StatObjectAsync(statObjectArgs);
  65. PresignedGetObjectArgs args = new PresignedGetObjectArgs()
  66. .WithBucket(Nowfd.bucket)
  67. .WithObject(Nowfd.objectName)
  68. .WithExpiry(60 * 60 * 24);
  69. string url = await minio.PresignedGetObjectAsync(args);
  70. RoomFileData rfd = new RoomFileData();
  71. rfd.url = url;
  72. RoomFileDataBack rfdb = new RoomFileDataBack();
  73. rfdb.rfd = rfd;
  74. rfdb.Nowfd = Nowfd;
  75. backQueue.Enqueue(rfdb);
  76. /*
  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. });
  100. await minio.GetObjectAsync(getObjectArgs);*/
  101. }
  102. void initMinIo(Action<bool> callBack)
  103. {
  104. JsonData data = new JsonData();
  105. data["roomId"] = projectId;
  106. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.storage_roomCredential, data.ToJson(), async (string str) =>
  107. {
  108. try
  109. {
  110. JsonData d = JsonMapper.ToObject(str);
  111. minioToken = d["data"]["credentials"]["token"].ToString();
  112. tmpSecretId = d["data"]["credentials"]["tmpSecretId"].ToString();
  113. tmpSecretKey = d["data"]["credentials"]["tmpSecretKey"].ToString();
  114. host = d["data"]["host"].ToString();
  115. bucket = d["data"]["bucket"].ToString();
  116. path = d["data"]["path"].ToString();
  117. Debug.Log(str);
  118. bool isHttps = host.Contains("https");
  119. var endpoint = host.Split("//")[1];
  120. var accessKey = tmpSecretId;//"tr6Nh5D8bnlGaLJE6vb5";
  121. var secretKey = tmpSecretKey;// "aVOYdXLnX4MCiKbit8aomZNWvAx8YSpzhiwzFhrI";
  122. Debug.Log("endpoint===>" + endpoint + " isHttps==>" + isHttps);
  123. minio = new MinioClient()
  124. .WithEndpoint(endpoint)
  125. .WithCredentials(accessKey, secretKey)
  126. .WithSessionToken(minioToken)
  127. .WithSSL(isHttps)
  128. .Build();
  129. TimerMgr.Instance.CreateTimer(() =>
  130. {
  131. minio = null;
  132. }, 1200);
  133. callBack?.Invoke(true);
  134. }
  135. catch
  136. {
  137. callBack?.Invoke(false);
  138. }
  139. }));
  140. }
  141. Queue<GetFileData> fdQueue = new Queue<GetFileData>();
  142. public void getFile(string roomId, string bucket, string objectName, Action<RoomFileData> callBack)//, string path
  143. {
  144. if (this.projectId!= roomId)
  145. {
  146. this.projectId = roomId;
  147. minio = null;
  148. }
  149. GetFileData getFileData = new GetFileData();
  150. getFileData.bucket = bucket;
  151. getFileData.objectName = objectName;
  152. getFileData.callBack = callBack;
  153. fdQueue.Enqueue(getFileData);
  154. }
  155. public class RoomFileData
  156. {
  157. public byte[] bytes;
  158. public string url;
  159. }
  160. public class RoomFileDataBack
  161. {
  162. public RoomFileData rfd;
  163. public GetFileData Nowfd;
  164. }
  165. GetFileData Nowfd;
  166. private void Update()
  167. {
  168. if (fdQueue.Count > 0 && Nowfd == null)
  169. {
  170. Nowfd = fdQueue.Dequeue();
  171. }
  172. if (minio == null)
  173. {
  174. if (projectId !=null && !isminioUpdate)
  175. {
  176. isminioUpdate = true;
  177. initMinIo((bool b) =>
  178. {
  179. isminioUpdate = false;
  180. });
  181. }
  182. }
  183. if (backQueue.Count > 0 )
  184. {
  185. RoomFileDataBack rfdb = backQueue.Dequeue();
  186. try
  187. {
  188. rfdb.Nowfd.callBack?.Invoke(rfdb.rfd);
  189. }
  190. catch
  191. {
  192. }
  193. isUpdate = false;
  194. Nowfd = null;
  195. }
  196. }
  197. public class GetFileData
  198. {
  199. public string bucket;
  200. public string objectName;
  201. public Action<RoomFileData> callBack;
  202. }
  203. }