TestMinIO.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using LitJson;
  2. using Minio;
  3. using Minio.DataModel.Args;
  4. using Minio.DataModel.Encryption;
  5. using Minio.Exceptions;
  6. using System;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. using UnityEngine;
  10. public class TestMinIO : MonoBehaviour
  11. {
  12. private string endpoint;
  13. private string accessKey;
  14. private string secretKey;
  15. private string minioToken;
  16. private string bucketName;
  17. private string objectName;
  18. private string contentType;
  19. private string tmpSecretId;
  20. private string tmpSecretKey;
  21. private string host;
  22. private string path;
  23. private MinioClient minio;
  24. private void Start()
  25. {
  26. endpoint = "api-fat2.ghz-tech.com";
  27. accessKey = "2RB1QSM6ZTPQPJIA32K3";
  28. secretKey = "c1e274Rj0ObD10cHOygHXV2wMZHcFubes0jOza6r";
  29. minioToken = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3NLZXkiOiIyUkIxUVNNNlpUUFFQSklBMzJLMyIsImV4cCI6MTY5OTgwOTE0MSwicGFyZW50IjoiZ2h6Iiwic2Vzc2lvblBvbGljeSI6ImV5SjJaWEp6YVc5dUlqb2lNakF4TWkweE1DMHhOeUlzSW5OMFlYUmxiV1Z1ZENJNlczc2lZV04wYVc5dUlqcGJJbk16T2tkbGRFSjFZMnRsZEV4dlkyRjBhVzl1SWl3aWN6TTZSMlYwVDJKcVpXTjBJaXdpY3pNNlVIVjBUMkpxWldOMElpd2ljek02VEdsemRFSjFZMnRsZENKZExDSmxabVpsWTNRaU9pSkJiR3h2ZHlJc0luSmxjMjkxY21ObElqcGJJbUZ5YmpwaGQzTTZjek02T2pvcUlsMTlYWDA9In0.SNxgRP-vBxKvBRwo2pWGcpD3Yap7bjN34jbsluY4pEa45zPmyRSWW-PxvMc69lTaXub2ijYgyPFwOe0iVi7ZMQ";
  30. bucketName = "ghz";
  31. objectName = "110901.jpg";
  32. contentType = "application/octet-stream";
  33. Active();
  34. /*
  35. */
  36. InspectionCredential();
  37. }
  38. private void InspectionCredential()
  39. {
  40. JsonData data = new JsonData();
  41. data["inspectionId"] = 10;
  42. GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp("/cmcc-endustry/v1/storage/inspectionCredential", data.ToJson(), async (string str) =>
  43. {
  44. JsonData d = JsonMapper.ToObject(str);
  45. //minioToken = d["data"]["credentials"]["token"].ToString();
  46. tmpSecretId = d["data"]["credentials"]["tmpSecretId"].ToString();
  47. //tmpSecretKey = d["data"]["credentials"]["tmpSecretKey"].ToString();
  48. host = d["data"]["host"].ToString();
  49. bucketName = d["data"]["bucket"].ToString();
  50. path = d["data"]["path"].ToString();
  51. Debug.Log(str);
  52. bool isHttps = host.Contains("https");
  53. //var endpoint = host.Split("//")[1];
  54. //var accessKey = tmpSecretId;//"tr6Nh5D8bnlGaLJE6vb5";
  55. //var secretKey = tmpSecretKey;// "aVOYdXLnX4MCiKbit8aomZNWvAx8YSpzhiwzFhrI";
  56. //Active();
  57. minio = (MinioClient)new MinioClient()
  58. .WithEndpoint(endpoint)
  59. .WithCredentials(accessKey, secretKey)
  60. .WithSessionToken(minioToken)
  61. .WithSSL(isHttps)
  62. .Build();
  63. // Run((MinioClient)minio).Wait();
  64. var getListBucketsTask = await minio.ListBucketsAsync().ConfigureAwait(false);
  65. foreach (var bucket in getListBucketsTask.Buckets)
  66. {
  67. Debug.LogError($"Bucket:{bucket.Name}");
  68. }
  69. Active();
  70. }));
  71. }
  72. private void Active()
  73. {
  74. try
  75. {
  76. /*
  77. var minio = (MinioClient)new MinioClient()
  78. //.WithEndpoint(endpoint)
  79. .WithEndpoint(host)
  80. .WithCredentials(accessKey, secretKey)
  81. .WithSessionToken(minioToken)
  82. .WithSSL()
  83. .Build();
  84. */
  85. SetObjectRun(minio).Wait();
  86. //GetObjectRun(minio, "bluebucketone", "BlueIOTest.jpg", "C:\\Users\\lsxk0\\Desktop\\DownAPK\\1107.jpg").Wait();
  87. }
  88. catch (Exception ex)
  89. {
  90. Debug.LogError("host:"+ host);
  91. Debug.LogError("bucketName:" + bucketName);
  92. Debug.LogError("path:" + path);
  93. Debug.LogError($"SetObjectRun Error----{ex.Message}");
  94. }
  95. }
  96. /// <summary>
  97. /// 文件上传
  98. /// </summary>
  99. private async Task SetObjectRun(MinioClient minio)
  100. {
  101. Debug.LogError("SetObjectRun");
  102. //var location = "us-east-1";
  103. var filePath = "C:\\Users\\lsxk0\\Desktop\\图片\\IMG_20190113_082755.jpg";
  104. //var filePath = Application.persistentDataPath + "/IMG.jpg";
  105. try
  106. {
  107. // 在服务器上制作一个bucket(如果还没有)
  108. var beArgs = new BucketExistsArgs()
  109. .WithBucket(bucketName);
  110. bool found = await minio.BucketExistsAsync(beArgs).ConfigureAwait(false);
  111. if (!found)
  112. {
  113. var mbArgs = new MakeBucketArgs()
  114. .WithBucket(bucketName);
  115. await minio.MakeBucketAsync(mbArgs).ConfigureAwait(false);
  116. }
  117. // 将文件上传到bucket
  118. var putObjectArgs = new PutObjectArgs()
  119. .WithBucket(bucketName)
  120. .WithObject(objectName)
  121. .WithFileName(filePath)
  122. .WithContentType(contentType);
  123. await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
  124. Debug.LogError("Successfully uploaded " + objectName);
  125. }
  126. catch (MinioException e)
  127. {
  128. Debug.LogError("Upload Error----");
  129. Debug.LogError($"File Upload Error: {e.Message}");
  130. }
  131. }
  132. /// <summary>
  133. /// 将对象从bucket下载到本地文件
  134. /// </summary>
  135. public async Task GetObjectRun(
  136. IMinioClient minio,
  137. string bucketName = "my-bucket-name",
  138. string objectName = "my-object-name",
  139. string fileName = "local-filename",
  140. IServerSideEncryption sse = null)
  141. {
  142. try
  143. {
  144. File.Delete(fileName);
  145. var args = new GetObjectArgs()
  146. .WithBucket(bucketName)
  147. .WithObject(objectName)
  148. .WithFile(fileName)
  149. .WithServerSideEncryption(sse);
  150. _ = await minio.GetObjectAsync(args).ConfigureAwait(false);
  151. Debug.LogError($"Downloaded the file {fileName} from bucket {bucketName}");
  152. }
  153. catch (Exception e)
  154. {
  155. Debug.LogError($"[Bucket] Exception: {e}");
  156. }
  157. }
  158. }