using LitJson; using Minio; using Minio.DataModel.Args; using Minio.Exceptions; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; using XRTool.Util; public class LangChaoRommMinIo : MonoSingleton { IMinioClient minio; string path; string bucket; string host; string tmpSecretId; string tmpSecretKey; string minioToken; string projectId; string region; Thread thread; private void Start() { thread = new Thread(DoSomeWork); thread.Start(); } bool isminioUpdate; bool isUpdate; void DoSomeWork() { while (true) { if (minio == null) { } else { if (Nowfd != null && !isUpdate) { isUpdate = true; Run().Wait(); } } Thread.Sleep(1000); } } public static byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); stream.Seek(0, SeekOrigin.Begin); return bytes; } Queue backQueue = new Queue(); // File uploader task. private async Task Run() { /* StatObjectArgs statObjectArgs = new StatObjectArgs() .WithBucket(Nowfd.bucket) .WithObject(Nowfd.objectName); await minio.StatObjectAsync(statObjectArgs); PresignedGetObjectArgs args = new PresignedGetObjectArgs() .WithBucket(Nowfd.bucket) .WithObject(Nowfd.objectName) .WithExpiry(60 * 60 * 24); string url = await minio.PresignedGetObjectAsync(args); RoomFileData rfd = new RoomFileData(); rfd.url = url; RoomFileDataBack rfdb = new RoomFileDataBack(); rfdb.rfd = rfd; rfdb.Nowfd = Nowfd; backQueue.Enqueue(rfdb);*/ StatObjectArgs statObjectArgs = new StatObjectArgs() .WithBucket(Nowfd.bucket) .WithObject(Nowfd.objectName); //.WithBucket("cmcc") //.WithObject("remote/845995/eb00bda9-c63b-4b93-a801-e45548e7a8c2.pdf"); await minio.StatObjectAsync(statObjectArgs); // Get input stream to have content of 'my-objectname' from 'my-bucketname' GetObjectArgs getObjectArgs = new GetObjectArgs() .WithBucket(Nowfd.bucket) .WithObject(Nowfd.objectName) .WithCallbackStream(async (stream) => { PresignedGetObjectArgs args = new PresignedGetObjectArgs() .WithBucket(Nowfd.bucket) .WithObject(Nowfd.objectName) .WithExpiry(60 * 60 * 24); string url = await minio.PresignedGetObjectAsync(args); RoomFileData rfd = new RoomFileData(); rfd.url = url; rfd.bytes = StreamToBytes(stream); RoomFileDataBack rfdb = new RoomFileDataBack(); rfdb.rfd = rfd; rfdb.Nowfd = Nowfd; backQueue.Enqueue(rfdb); rfd.url = Application.persistentDataPath + "/" + Path.GetFileName(url); }); await minio.GetObjectAsync(getObjectArgs); } void initMinIo(Action callBack) { JsonData data = new JsonData(); data["roomId"] = projectId; StartCoroutine(HttpToolLangChao.Instance.SendHttp(HttpActionLang.storage_roomCredential, data.ToJson(), async (string str) => { try { JsonData d = JsonMapper.ToObject(str); minioToken = d["data"]["credentials"]["token"].ToString(); tmpSecretId = d["data"]["credentials"]["tmpSecretId"].ToString(); tmpSecretKey = d["data"]["credentials"]["tmpSecretKey"].ToString(); host = d["data"]["host"].ToString(); bucket = d["data"]["bucket"].ToString(); path = d["data"]["path"].ToString(); Debug.Log(str); bool isHttps = host.Contains("https"); var endpoint = host.Split("//")[1]; var accessKey = tmpSecretId;//"tr6Nh5D8bnlGaLJE6vb5"; var secretKey = tmpSecretKey;// "aVOYdXLnX4MCiKbit8aomZNWvAx8YSpzhiwzFhrI"; Debug.Log("endpoint===>" + endpoint + " isHttps==>" + isHttps); //if(d["data"]["region"]!=null&& !string.IsNullOrEmpty(d["data"]["region"].ToString())&& d["data"]["region"].ToString()!="null") //{ // region = d["data"]["region"].ToString(); // minio = new MinioClient() // .WithEndpoint(endpoint) // .WithCredentials(accessKey, secretKey) // .WithSessionToken(minioToken) // .WithSSL(isHttps) // .WithRegion(region) // .Build(); //} //else { minio = new MinioClient() .WithEndpoint(endpoint) .WithCredentials(accessKey, secretKey) .WithSessionToken(minioToken) .WithSSL(isHttps) .Build(); } Debug.Log(minio == null); TimerMgr.Instance.CreateTimer(() => { minio = null; }, 3000); callBack?.Invoke(true); } catch { callBack?.Invoke(false); } })); } Queue fdQueue = new Queue(); public void getFile(string roomId, string bucket, string objectName, Action callBack)//, string path { if (this.projectId!= roomId) { this.projectId = roomId; // minio = null; } //Unity 当执行大量null 引用操作的时候 下面的方法比 gameobject!=null 快了大概两倍 if (!System.Object.ReferenceEquals(gameObject, null)) { } GetFileData getFileData = new GetFileData(); getFileData.bucket = bucket; getFileData.objectName = objectName; getFileData.callBack = callBack; fdQueue.Enqueue(getFileData); } public class RoomFileData { public byte[] bytes; public string url; } public class RoomFileDataBack { public RoomFileData rfd; public GetFileData Nowfd; } GetFileData Nowfd; private void Update() { if (fdQueue.Count > 0 && Nowfd == null) { Nowfd = fdQueue.Dequeue(); } if (minio == null) { if (projectId !=null && !isminioUpdate) { isminioUpdate = true; initMinIo((bool b) => { isminioUpdate = false; }); } } if (backQueue.Count > 0 ) { RoomFileDataBack rfdb = backQueue.Dequeue(); try { rfdb.Nowfd.callBack?.Invoke(rfdb.rfd); } catch { } isUpdate = false; Nowfd = null; } } public class GetFileData { public string bucket; public string objectName; public Action callBack; } }