123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- using LitJson;
- using Minio;
- using Minio.DataModel.Args;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading;
- using System.Threading.Tasks;
- using UnityEngine;
- using XRTool.Util;
- public class RoomFileMinio : MonoSingleton<RoomFileMinio>
- {
- IMinioClient minio;
- string path;
- public string bucket;
- public bool islogin;
- string host;
- string region;
- string tmpSecretId;
- string tmpSecretKey;
- string minioToken;
-
- string projectId;
- Thread thread;
- private void Start()
- {
- thread = new Thread(DoSomeWork);
- thread.Start();
- islogin = false;
- }
- bool isminioUpdate;
- bool isUpdate;
- bool isUpload;
- void DoSomeWork()
- {
- while (true)
- {
- if (minio == null)
- {
- }
- else
- {
- if (Nowfd != null && !isUpdate)
- {
- isUpdate = true;
- Run().Wait();
- }
- if(NowPfd!=null && !isUpload)
- {
- isUpload = true;
- UploadFile().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<RoomFileDataBack> backQueue = new Queue<RoomFileDataBack>();
- // 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);
- 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);
- }
- private async Task UploadFile()
- {
- // await minio.ListBucketsAsync().ConfigureAwait(false);
- Dictionary<string, string> dicheaders = new Dictionary<string, string>();
- dicheaders.Add("X-Amz-Meta-Uuid", NowPfd.uuid);
- Debug.Log(NowPfd.bucket + " " + NowPfd.filePath + " " + NowPfd.objectName + " " + NowPfd.fileSize);
- PutObjectArgs putObjectArgs = new PutObjectArgs()
- .WithBucket(NowPfd.bucket)
- .WithObject(NowPfd.objectName)
- .WithFileName(NowPfd.filePath)
- .WithContentType("application/octet-stream")
- .WithObjectSize(NowPfd.fileSize)
- .WithHeaders(dicheaders);
-
- await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
- NowPfd.callBack?.Invoke(NowPfd.objectName);
- NowPfd = null;
- try
- {
-
- }
- catch (Exception)
- {
- Nowfd.callBack?.Invoke(null);
- Nowfd = null;
- }
- isUpload = false;
- }
- void initMinIo(Action<bool> callBack)
- {
-
- //获取上传凭证, 根据上传凭证获取的数据初始化 MInio
- StartCoroutine(HttpTool.Instance.SendHttp(HttpEdustryAction.storage_projectCredential, "", async (string str) =>
- {
- Debug.Log("DGJ ===> initMinIo " + 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();
- }
- TimerMgr.Instance.CreateTimer(() =>
- {
- minio = null;
- }, 1800);
- callBack?.Invoke(true);
- }
- catch
- {
- callBack?.Invoke(false);
- }
- }));
- }
- Queue<GetFileData> fdQueue = new Queue<GetFileData>();
- public void getFile( string bucket, string objectName, Action<RoomFileData> callBack, string roomId=null)//, string path
- {
- Debug.Log(" DGJ getFile " +bucket+" "+ objectName);
- if (this.projectId != roomId)
- {
- this.projectId = roomId;
- // minio = null;
- }
- GetFileData getFileData = new GetFileData();
- getFileData.bucket = bucket;
- getFileData.objectName = objectName;
- getFileData.callBack = callBack;
- fdQueue.Enqueue(getFileData);
- }
- Queue<PutFileData> pdQueue = new Queue<PutFileData>();
- public void PutFile(string bucket ,string filePath, string fileName,string objectName, string uuid ,int fileSize,Action<string> callBack)
- {
- Debug.Log("DGJ PutFile" + bucket + " " + filePath);
- PutFileData putFileData = new PutFileData();
- putFileData.bucket = bucket;
- putFileData.filePath = filePath;
- putFileData.fileName = fileName;
- putFileData.fileSize = fileSize;
- putFileData.callBack = callBack;
- putFileData.objectName = objectName;
- putFileData.uuid = uuid;
- pdQueue.Enqueue(putFileData);
- }
- public class RoomFileData
- {
- public byte[] bytes;
- public string url;
- }
- public class RoomFileDataBack
- {
- public RoomFileData rfd;
- public GetFileData Nowfd;
- }
- GetFileData Nowfd;
- PutFileData NowPfd;
- private void Update()
- {
- if (fdQueue.Count > 0 && Nowfd == null)
- {
- Nowfd = fdQueue.Dequeue();
- }
- if(pdQueue.Count>0 && NowPfd ==null)
- {
- NowPfd = pdQueue.Dequeue();
- }
- if (minio == null)
- {
- if (islogin&&!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<RoomFileData> callBack;
- }
- public class PutFileData
- {
- public string bucket;
- public string filePath;
- public string fileName;
- public int fileSize;
- public string objectName;
- public string uuid;
- public Action<string> callBack;
- }
- }
|