DownLoadMinIOXRItem.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using Minio;
  6. using Minio.DataModel.Args;
  7. using UnityEngine;
  8. using static DownLoadXRManager;
  9. public class DownLoadMinIOXRItem : MonoBehaviour
  10. {
  11. public DownLoadConfig minioconifg;
  12. public PutFileData uploadconifg;
  13. public IMinioClient minio;
  14. public static Dictionary<string, List<DownLoadConfig>> downLoadingList = new Dictionary<string, List<DownLoadConfig>>();
  15. public void startDownload(DownLoadConfig conifg)
  16. {
  17. minioconifg = conifg;
  18. InitMaterialMinio();
  19. StartCoroutine(ReadStreamingAssetsFile());
  20. }
  21. public void startUpLoad(PutFileData conifg)
  22. {
  23. uploadconifg = conifg;
  24. InitMaterialMinio();
  25. StartCoroutine(ReadStreamingUpLoad());
  26. }
  27. private void InitMaterialMinio()
  28. {
  29. bool isHttps = cospingzheng.host.Contains("https");
  30. var endpoint = cospingzheng.host.Split("//")[1];
  31. var accessKey = cospingzheng.credentials.tmpSecretId;//"tr6Nh5D8bnlGaLJE6vb5";
  32. var secretKey = cospingzheng.credentials.tmpSecretKey;// "aVOYdXLnX4MCiKbit8aomZNWvAx8YSpzhiwzFhrI";
  33. var minioToken = cospingzheng.credentials.token;
  34. if (cospingzheng.region != null && !string.IsNullOrEmpty(cospingzheng.region) && cospingzheng.region != "null")
  35. {
  36. minio = new MinioClient()
  37. .WithEndpoint(endpoint)
  38. .WithCredentials(accessKey, secretKey)
  39. .WithSessionToken(minioToken)
  40. .WithSSL(isHttps)
  41. .WithRegion(cospingzheng.region)
  42. .Build();
  43. }
  44. else
  45. {
  46. minio = new MinioClient()
  47. .WithEndpoint(endpoint)
  48. .WithCredentials(accessKey, secretKey)
  49. .WithSessionToken(minioToken)
  50. .WithSSL(isHttps)
  51. .Build();
  52. }
  53. }
  54. IEnumerator ReadStreamingUpLoad()
  55. {
  56. //.cssg-snippet-body-start:[transfer-batch-download-objects]
  57. // await minio.ListBucketsAsync().ConfigureAwait(false);
  58. Dictionary<string, string> dicheaders = new Dictionary<string, string>();
  59. dicheaders.Add("X-Amz-Meta-Uuid", uploadconifg.uuid);
  60. Debug.Log(cospingzheng.bucket + " " + uploadconifg.filePath + " " + uploadconifg.objectName + " " + uploadconifg.fileSize);
  61. PutObjectArgs putObjectArgs = new PutObjectArgs()
  62. .WithBucket(cospingzheng.bucket)
  63. .WithObject(uploadconifg.objectName)
  64. .WithFileName(uploadconifg.filePath)
  65. .WithContentType("application/octet-stream")
  66. .WithObjectSize(uploadconifg.fileSize)
  67. .WithHeaders(dicheaders);
  68. yield return minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
  69. uploadconifg.callBack?.Invoke(uploadconifg.objectName);
  70. }
  71. public string localPath;
  72. IEnumerator ReadStreamingAssetsFile()
  73. {
  74. if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR/"))
  75. {
  76. Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR/");
  77. }
  78. Debug.Log("ReadStreamingAssetsFile==cospingzheng.bucket=>" + cospingzheng.bucket +
  79. " cosconifg.data.path==>" + minioconifg.data.path +
  80. " localPathh==>" + localPath );
  81. StatObjectArgs statObjectArgs = new StatObjectArgs()
  82. .WithBucket(cospingzheng.bucket)
  83. .WithObject(minioconifg.data.path);
  84. yield return minio.StatObjectAsync(statObjectArgs);
  85. byte[] bystes = null;
  86. Debug.Log(" miniominiominiominiominiominiominiominiominiominio");
  87. // Get input stream to have content of 'my-objectname' from 'my-bucketname'
  88. GetObjectArgs getObjectArgs = new GetObjectArgs()
  89. .WithBucket(cospingzheng.bucket)
  90. .WithObject(minioconifg.data.path)
  91. .WithCallbackStream((stream) =>
  92. {
  93. Debug.Log(" WithCallbackStreamWithCallbackStreamWithCallbackStreamWithCallbackStreamWithCallbackStream");
  94. bystes = StreamToBytes(stream);
  95. taskQueue.Enqueue(bystes);
  96. /**/
  97. // rfd.url = Application.persistentDataPath + "/" + Path.GetFileName(url);
  98. })
  99. ;
  100. yield return minio.GetObjectAsync(getObjectArgs);
  101. /*
  102. Debug.Log(" DGJ getFile DoSomeWork RunBack");
  103. PresignedGetObjectArgs args = new PresignedGetObjectArgs()
  104. .WithBucket(cospingzheng.bucket)
  105. .WithObject(minioconifg.data.path)
  106. .WithExpiry(60 * 60 * 24);
  107. Task<string> tk = minio.PresignedGetObjectAsync(args);
  108. Debug.Log(" DGJ getFile DoSomeWork RunBack");
  109. while (tk.Result == null || tk.Result == "" || bystes == null)
  110. {
  111. yield return null;
  112. }
  113. Debug.Log(" DGJ getFile DoSomeWork rfdb" + tk.Result);
  114. string url = tk.Result;
  115. rfd.url = url;
  116. Debug.Log(" DGJ getFile DoSomeWork rfdb");
  117. RoomFileDataBack rfdb = new RoomFileDataBack();
  118. rfdb.rfd = rfd;
  119. rfdb.Nowfd = Nowfd;
  120. backQueue.Enqueue(rfdb);
  121. // Destroy(this.gameObject);*/
  122. }
  123. public static byte[] StreamToBytes(Stream stream)
  124. {
  125. byte[] bytes = new byte[stream.Length];
  126. stream.Read(bytes, 0, bytes.Length);
  127. stream.Seek(0, SeekOrigin.Begin);
  128. return bytes;
  129. }
  130. public Queue<byte[]> taskQueue = new Queue<byte[]>();
  131. public byte[] readbytes(Stream stream)
  132. {
  133. using (Stream fs = stream)
  134. {
  135. byte[] bytes = new byte[fs.Length];
  136. fs.Read(bytes, 0, bytes.Length);
  137. fs.Close();
  138. return bytes;
  139. }
  140. }
  141. private void Update()
  142. {
  143. if (taskQueue.Count > 0)
  144. {
  145. var state = taskQueue.Dequeue();
  146. minioconifg.bytes(state);
  147. }
  148. }
  149. }