RoomFileMinio.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using LitJson;
  2. using Minio;
  3. using Minio.DataModel.Args;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using UnityEngine;
  11. using XRTool.Util;
  12. public class RoomFileMinio : MonoSingleton<RoomFileMinio>
  13. {
  14. IMinioClient minio;
  15. string path;
  16. public string bucket;
  17. public bool islogin;
  18. string host;
  19. string region;
  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. islogin = false;
  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. await minio.StatObjectAsync(statObjectArgs);
  82. // Get input stream to have content of 'my-objectname' from 'my-bucketname'
  83. GetObjectArgs getObjectArgs = new GetObjectArgs()
  84. .WithBucket(Nowfd.bucket)
  85. .WithObject(Nowfd.objectName)
  86. .WithCallbackStream(async (stream) =>
  87. {
  88. PresignedGetObjectArgs args = new PresignedGetObjectArgs()
  89. .WithBucket(Nowfd.bucket)
  90. .WithObject(Nowfd.objectName)
  91. .WithExpiry(60 * 60 * 24);
  92. string url = await minio.PresignedGetObjectAsync(args);
  93. RoomFileData rfd = new RoomFileData();
  94. rfd.url = url;
  95. rfd.bytes = StreamToBytes(stream);
  96. RoomFileDataBack rfdb = new RoomFileDataBack();
  97. rfdb.rfd = rfd;
  98. rfdb.Nowfd = Nowfd;
  99. backQueue.Enqueue(rfdb);
  100. // rfd.url = Application.persistentDataPath + "/" + Path.GetFileName(url);
  101. });
  102. await minio.GetObjectAsync(getObjectArgs);
  103. }
  104. void initMinIo(Action<bool> callBack)
  105. {
  106. //获取上传凭证, 根据上传凭证获取的数据初始化 MInio
  107. StartCoroutine(HttpTool.Instance.SendHttp(HttpEdustryAction.storage_projectCredential, "", async (string str) =>
  108. {
  109. Debug.Log("DGJ ===> initMinIo " + str);
  110. try
  111. {
  112. JsonData d = JsonMapper.ToObject(str);
  113. minioToken = d["data"]["credentials"]["token"].ToString();
  114. tmpSecretId = d["data"]["credentials"]["tmpSecretId"].ToString();
  115. tmpSecretKey = d["data"]["credentials"]["tmpSecretKey"].ToString();
  116. host = d["data"]["host"].ToString();
  117. bucket = d["data"]["bucket"].ToString();
  118. // path = d["data"]["path"].ToString();
  119. Debug.Log(str);
  120. bool isHttps = host.Contains("https");
  121. var endpoint = host.Split("//")[1];
  122. var accessKey = tmpSecretId;//"tr6Nh5D8bnlGaLJE6vb5";
  123. var secretKey = tmpSecretKey;// "aVOYdXLnX4MCiKbit8aomZNWvAx8YSpzhiwzFhrI";
  124. Debug.Log("endpoint===>" + endpoint + " isHttps==>" + isHttps);
  125. if (d["data"]["region"] != null && !string.IsNullOrEmpty(d["data"]["region"].ToString()) && d["data"]["region"].ToString() != "null")
  126. {
  127. region = d["data"]["region"].ToString();
  128. minio = new MinioClient()
  129. .WithEndpoint(endpoint)
  130. .WithCredentials(accessKey, secretKey)
  131. .WithSessionToken(minioToken)
  132. .WithSSL(isHttps)
  133. .WithRegion(region)
  134. .Build();
  135. }
  136. else
  137. {
  138. minio = new MinioClient()
  139. .WithEndpoint(endpoint)
  140. .WithCredentials(accessKey, secretKey)
  141. .WithSessionToken(minioToken)
  142. .WithSSL(isHttps)
  143. .Build();
  144. }
  145. TimerMgr.Instance.CreateTimer(() =>
  146. {
  147. minio = null;
  148. }, 1800);
  149. callBack?.Invoke(true);
  150. }
  151. catch
  152. {
  153. callBack?.Invoke(false);
  154. }
  155. }));
  156. }
  157. Queue<GetFileData> fdQueue = new Queue<GetFileData>();
  158. public void getFile( string bucket, string objectName, Action<RoomFileData> callBack, string roomId=null)//, string path
  159. {
  160. Debug.Log(" DGJ getFile " +bucket+" "+ objectName);
  161. if (this.projectId != roomId)
  162. {
  163. this.projectId = roomId;
  164. // minio = null;
  165. }
  166. GetFileData getFileData = new GetFileData();
  167. getFileData.bucket = bucket;
  168. getFileData.objectName = objectName;
  169. getFileData.callBack = callBack;
  170. fdQueue.Enqueue(getFileData);
  171. }
  172. public class RoomFileData
  173. {
  174. public byte[] bytes;
  175. public string url;
  176. }
  177. public class RoomFileDataBack
  178. {
  179. public RoomFileData rfd;
  180. public GetFileData Nowfd;
  181. }
  182. GetFileData Nowfd;
  183. private void Update()
  184. {
  185. if (fdQueue.Count > 0 && Nowfd == null)
  186. {
  187. Nowfd = fdQueue.Dequeue();
  188. }
  189. if (minio == null)
  190. {
  191. if (islogin&&!isminioUpdate)
  192. {
  193. isminioUpdate = true;
  194. initMinIo((bool b) =>
  195. {
  196. isminioUpdate = false;
  197. });
  198. }
  199. }
  200. if (backQueue.Count > 0)
  201. {
  202. RoomFileDataBack rfdb = backQueue.Dequeue();
  203. try
  204. {
  205. rfdb.Nowfd.callBack?.Invoke(rfdb.rfd);
  206. }
  207. catch
  208. {
  209. }
  210. isUpdate = false;
  211. Nowfd = null;
  212. }
  213. }
  214. public class GetFileData
  215. {
  216. public string bucket;
  217. public string objectName;
  218. public Action<RoomFileData> callBack;
  219. }
  220. }