RoomFileMinio.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. bool isUpload;
  34. void DoSomeWork()
  35. {
  36. while (true)
  37. {
  38. if (minio == null)
  39. {
  40. }
  41. else
  42. {
  43. if (Nowfd != null && !isUpdate)
  44. {
  45. isUpdate = true;
  46. Run().Wait();
  47. }
  48. if(NowPfd!=null && !isUpload)
  49. {
  50. isUpload = true;
  51. UploadFile().Wait();
  52. }
  53. }
  54. Thread.Sleep(1000);
  55. }
  56. }
  57. public static byte[] StreamToBytes(Stream stream)
  58. {
  59. byte[] bytes = new byte[stream.Length];
  60. stream.Read(bytes, 0, bytes.Length);
  61. stream.Seek(0, SeekOrigin.Begin);
  62. return bytes;
  63. }
  64. Queue<RoomFileDataBack> backQueue = new Queue<RoomFileDataBack>();
  65. // File uploader task.
  66. private async Task Run()
  67. {
  68. /*
  69. StatObjectArgs statObjectArgs = new StatObjectArgs()
  70. .WithBucket(Nowfd.bucket)
  71. .WithObject(Nowfd.objectName);
  72. await minio.StatObjectAsync(statObjectArgs);
  73. PresignedGetObjectArgs args = new PresignedGetObjectArgs()
  74. .WithBucket(Nowfd.bucket)
  75. .WithObject(Nowfd.objectName)
  76. .WithExpiry(60 * 60 * 24);
  77. string url = await minio.PresignedGetObjectAsync(args);
  78. RoomFileData rfd = new RoomFileData();
  79. rfd.url = url;
  80. RoomFileDataBack rfdb = new RoomFileDataBack();
  81. rfdb.rfd = rfd;
  82. rfdb.Nowfd = Nowfd;
  83. backQueue.Enqueue(rfdb);*/
  84. StatObjectArgs statObjectArgs = new StatObjectArgs()
  85. .WithBucket(Nowfd.bucket)
  86. .WithObject(Nowfd.objectName);
  87. await minio.StatObjectAsync(statObjectArgs);
  88. // Get input stream to have content of 'my-objectname' from 'my-bucketname'
  89. GetObjectArgs getObjectArgs = new GetObjectArgs()
  90. .WithBucket(Nowfd.bucket)
  91. .WithObject(Nowfd.objectName)
  92. .WithCallbackStream(async (stream) =>
  93. {
  94. PresignedGetObjectArgs args = new PresignedGetObjectArgs()
  95. .WithBucket(Nowfd.bucket)
  96. .WithObject(Nowfd.objectName)
  97. .WithExpiry(60 * 60 * 24);
  98. string url = await minio.PresignedGetObjectAsync(args);
  99. RoomFileData rfd = new RoomFileData();
  100. rfd.url = url;
  101. rfd.bytes = StreamToBytes(stream);
  102. RoomFileDataBack rfdb = new RoomFileDataBack();
  103. rfdb.rfd = rfd;
  104. rfdb.Nowfd = Nowfd;
  105. backQueue.Enqueue(rfdb);
  106. // rfd.url = Application.persistentDataPath + "/" + Path.GetFileName(url);
  107. });
  108. await minio.GetObjectAsync(getObjectArgs);
  109. }
  110. private async Task UploadFile()
  111. {
  112. // await minio.ListBucketsAsync().ConfigureAwait(false);
  113. Dictionary<string, string> dicheaders = new Dictionary<string, string>();
  114. dicheaders.Add("X-Amz-Meta-Uuid", NowPfd.uuid);
  115. Debug.Log(NowPfd.bucket + " " + NowPfd.filePath + " " + NowPfd.objectName + " " + NowPfd.fileSize);
  116. PutObjectArgs putObjectArgs = new PutObjectArgs()
  117. .WithBucket(NowPfd.bucket)
  118. .WithObject(NowPfd.objectName)
  119. .WithFileName(NowPfd.filePath)
  120. .WithContentType("application/octet-stream")
  121. .WithObjectSize(NowPfd.fileSize)
  122. .WithHeaders(dicheaders);
  123. await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
  124. NowPfd.callBack?.Invoke(NowPfd.objectName);
  125. NowPfd = null;
  126. try
  127. {
  128. }
  129. catch (Exception)
  130. {
  131. Nowfd.callBack?.Invoke(null);
  132. Nowfd = null;
  133. }
  134. isUpload = false;
  135. }
  136. void initMinIo(Action<bool> callBack)
  137. {
  138. //获取上传凭证, 根据上传凭证获取的数据初始化 MInio
  139. StartCoroutine(HttpTool.Instance.SendHttp(HttpEdustryAction.storage_projectCredential, "", async (string str) =>
  140. {
  141. Debug.Log("DGJ ===> initMinIo " + str);
  142. try
  143. {
  144. JsonData d = JsonMapper.ToObject(str);
  145. minioToken = d["data"]["credentials"]["token"].ToString();
  146. tmpSecretId = d["data"]["credentials"]["tmpSecretId"].ToString();
  147. tmpSecretKey = d["data"]["credentials"]["tmpSecretKey"].ToString();
  148. host = d["data"]["host"].ToString();
  149. bucket = d["data"]["bucket"].ToString();
  150. // path = d["data"]["path"].ToString();
  151. Debug.Log(str);
  152. bool isHttps = host.Contains("https");
  153. var endpoint = host.Split("//")[1];
  154. var accessKey = tmpSecretId;//"tr6Nh5D8bnlGaLJE6vb5";
  155. var secretKey = tmpSecretKey;// "aVOYdXLnX4MCiKbit8aomZNWvAx8YSpzhiwzFhrI";
  156. Debug.Log("endpoint===>" + endpoint + " isHttps==>" + isHttps);
  157. if (d["data"]["region"] != null && !string.IsNullOrEmpty(d["data"]["region"].ToString()) && d["data"]["region"].ToString() != "null")
  158. {
  159. region = d["data"]["region"].ToString();
  160. minio = new MinioClient()
  161. .WithEndpoint(endpoint)
  162. .WithCredentials(accessKey, secretKey)
  163. .WithSessionToken(minioToken)
  164. .WithSSL(isHttps)
  165. .WithRegion(region)
  166. .Build();
  167. }
  168. else
  169. {
  170. minio = new MinioClient()
  171. .WithEndpoint(endpoint)
  172. .WithCredentials(accessKey, secretKey)
  173. .WithSessionToken(minioToken)
  174. .WithSSL(isHttps)
  175. .Build();
  176. }
  177. TimerMgr.Instance.CreateTimer(() =>
  178. {
  179. minio = null;
  180. }, 1800);
  181. callBack?.Invoke(true);
  182. }
  183. catch
  184. {
  185. callBack?.Invoke(false);
  186. }
  187. }));
  188. }
  189. Queue<GetFileData> fdQueue = new Queue<GetFileData>();
  190. public void getFile( string bucket, string objectName, Action<RoomFileData> callBack, string roomId=null)//, string path
  191. {
  192. Debug.Log(" DGJ getFile " +bucket+" "+ objectName);
  193. if (this.projectId != roomId)
  194. {
  195. this.projectId = roomId;
  196. // minio = null;
  197. }
  198. GetFileData getFileData = new GetFileData();
  199. getFileData.bucket = bucket;
  200. getFileData.objectName = objectName;
  201. getFileData.callBack = callBack;
  202. fdQueue.Enqueue(getFileData);
  203. }
  204. Queue<PutFileData> pdQueue = new Queue<PutFileData>();
  205. public void PutFile(string bucket ,string filePath, string fileName,string objectName, string uuid ,int fileSize,Action<string> callBack)
  206. {
  207. Debug.Log("DGJ PutFile" + bucket + " " + filePath);
  208. PutFileData putFileData = new PutFileData();
  209. putFileData.bucket = bucket;
  210. putFileData.filePath = filePath;
  211. putFileData.fileName = fileName;
  212. putFileData.fileSize = fileSize;
  213. putFileData.callBack = callBack;
  214. putFileData.objectName = objectName;
  215. putFileData.uuid = uuid;
  216. pdQueue.Enqueue(putFileData);
  217. }
  218. public class RoomFileData
  219. {
  220. public byte[] bytes;
  221. public string url;
  222. }
  223. public class RoomFileDataBack
  224. {
  225. public RoomFileData rfd;
  226. public GetFileData Nowfd;
  227. }
  228. GetFileData Nowfd;
  229. PutFileData NowPfd;
  230. private void Update()
  231. {
  232. if (fdQueue.Count > 0 && Nowfd == null)
  233. {
  234. Nowfd = fdQueue.Dequeue();
  235. }
  236. if(pdQueue.Count>0 && NowPfd ==null)
  237. {
  238. NowPfd = pdQueue.Dequeue();
  239. }
  240. if (minio == null)
  241. {
  242. if (islogin&&!isminioUpdate)
  243. {
  244. isminioUpdate = true;
  245. initMinIo((bool b) =>
  246. {
  247. isminioUpdate = false;
  248. });
  249. }
  250. }
  251. if (backQueue.Count > 0)
  252. {
  253. RoomFileDataBack rfdb = backQueue.Dequeue();
  254. try
  255. {
  256. rfdb.Nowfd.callBack?.Invoke(rfdb.rfd);
  257. }
  258. catch
  259. {
  260. }
  261. isUpdate = false;
  262. Nowfd = null;
  263. }
  264. }
  265. public class GetFileData
  266. {
  267. public string bucket;
  268. public string objectName;
  269. public Action<RoomFileData> callBack;
  270. }
  271. public class PutFileData
  272. {
  273. public string bucket;
  274. public string filePath;
  275. public string fileName;
  276. public int fileSize;
  277. public string objectName;
  278. public string uuid;
  279. public Action<string> callBack;
  280. }
  281. }