From 774f57d116177d564040dcd0aed949c4aceda17d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BC=9F?= <421281095@qq.com> Date: Thu, 27 Mar 2025 10:22:59 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=A4=9A=E7=BA=BF=E7=A8=8B=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=A2=9E=E5=8A=A0=E9=BB=98=E8=AE=A4=E5=80=BC=202.=20?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E7=9B=91=E5=90=AC=E4=B8=8B=E8=BD=BD=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=94=AF=E6=8C=81=E5=A4=A7=E6=95=B0=E9=87=8F=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 6 +++++- Hopetry.csproj | 4 +++- Services/MinioService.cs | 37 ++++++++++++++++++++++++++----------- global.json | 9 +++++++++ 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index c136f39..e50e0e9 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -40,8 +40,12 @@ namespace HeBianGu.App.Disk if (File.Exists("./settings.xml")) { var setting = xmlSerializerService.Load("./settings.xml"); - if (!string.IsNullOrEmpty(setting.SyncDir) && setting.TaskCount != null) + if (!string.IsNullOrEmpty(setting.SyncDir)) { + if (setting.TaskCount == 0 ) + { + setting.TaskCount = 3; + } minioService.MirrorAsync1(bucketName, setting.SyncDir, setting.TaskCount); minioService.RealTimeListen(bucketName, setting.SyncDir); } diff --git a/Hopetry.csproj b/Hopetry.csproj index e708a1f..94fcddb 100644 --- a/Hopetry.csproj +++ b/Hopetry.csproj @@ -87,7 +87,9 @@ - + + Always + diff --git a/Services/MinioService.cs b/Services/MinioService.cs index de2043d..0bf943a 100644 --- a/Services/MinioService.cs +++ b/Services/MinioService.cs @@ -98,13 +98,13 @@ namespace Hopetry.Services /// public async Task MirrorAsync1(string bucket, string localDir, int maxParallel = 5) { - - Channel<(string ObjectName, string ETag)> SyncChannel = Channel.CreateBounded<(string, string)>(new BoundedChannelOptions(10000) - { - SingleWriter = false, - SingleReader = false, - FullMode = BoundedChannelFullMode.Wait - }); + Channel<(string ObjectName, string ETag)> SyncChannel = Channel.CreateBounded<(string, string)>( + new BoundedChannelOptions(10000) + { + SingleWriter = false, + SingleReader = false, + FullMode = BoundedChannelFullMode.Wait + }); var count = 0; var producerTasks = new List(); var listArgs = new ListObjectsArgs() @@ -253,7 +253,7 @@ namespace Hopetry.Services Console.WriteLine("mybucket does not exist"); } } - + public async Task DownLoadObject(string bucketName, string objectKey, string localDir, string objectETag) { var index = objectKey.LastIndexOf("/", StringComparison.Ordinal); @@ -314,10 +314,24 @@ namespace Hopetry.Services // 实时监听方法 public async Task RealTimeListen(string bucketName, string localDir, string prefix = "", string suffix = "") { + var downloadQueue = + Channel.CreateBounded<(string bucketName, string objectKey, string localDir, string objectETag)>( + new BoundedChannelOptions(1000) + { + FullMode = BoundedChannelFullMode.Wait + }); + Task.Run(async () => + { + await Parallel.ForEachAsync(downloadQueue.Reader.ReadAllAsync(), + new ParallelOptions { MaxDegreeOfParallelism = 5 }, + async (item, _) => + { + await DownLoadObject(item.bucketName, item.objectKey, item.localDir, item.objectETag); + }); + }); try { var events = new List { EventType.ObjectCreatedAll }; - ListenBucketNotificationsArgs args = new ListenBucketNotificationsArgs() .WithBucket(bucketName) .WithEvents(events); @@ -333,8 +347,9 @@ namespace Hopetry.Services var objectSize = obj["Records"]?[0]?["s3"]?["object"]?["size"]?.Value(); var objectKey = obj["Records"]?[0]?["s3"]?["object"]?["key"]?.Value(); var objectETag = obj["Records"]?[0]?["s3"]?["object"]?["eTag"]?.Value(); - - await DownLoadObject(bucketName, objectKey, localDir, objectETag); + Console.WriteLine("将要下载的Key:" + objectKey); + downloadQueue.Writer.TryWrite((bucketName, objectKey, localDir, objectETag)); + //await DownLoadObject(bucketName, objectKey, localDir, objectETag); }, ex => Console.WriteLine($"OnError: {ex}"), () => Console.WriteLine($"Stopped listening for bucket notifications\n")); diff --git a/global.json b/global.json index 07f3ce9..3030b8d 100644 --- a/global.json +++ b/global.json @@ -1,3 +1,4 @@ +/* { "Minio": { "Endpoint": "123.132.248.154:9107", @@ -5,4 +6,12 @@ "SecretKey": "66GYn0x1XAEInSa9wdCutzvUWKfhH1EhqxPJ6a9u", "BucketName": "demo" } +}*/ +{ + "Minio": { + "Endpoint": "192.168.10.163:9016", + "AccessKey": "I2c35jD6ayApaneyQZyC", + "SecretKey": "XHlrNeCHK0xf8y2Fo0K5OKyDeaI2ItfEsFbzQPFk", + "BucketName": "demo" + } } \ No newline at end of file