diff --git a/IRaCIS.Core.API/Controllers/ExtraController.cs b/IRaCIS.Core.API/Controllers/ExtraController.cs index e750b4727..baa34b64f 100644 --- a/IRaCIS.Core.API/Controllers/ExtraController.cs +++ b/IRaCIS.Core.API/Controllers/ExtraController.cs @@ -379,7 +379,7 @@ namespace IRaCIS.Api.Controllers SecretAccessKey = credentials.SecretAccessKey, SessionToken= credentials.SessionToken, Expiration=credentials.Expiration, - + Region=awsOptions.Region, BucketName = awsOptions.BucketName, EndPoint = awsOptions.EndPoint, @@ -422,7 +422,7 @@ namespace IRaCIS.Api.Controllers SecretAccessKey = credentials.SecretAccessKey, SessionToken = credentials.SessionToken, Expiration = credentials.Expiration, - + Region = awsOptions.Region, BucketName = awsOptions.BucketName, EndPoint = awsOptions.EndPoint, ViewEndpoint = awsOptions.ViewEndpoint, diff --git a/IRaCIS.Core.Application/Helper/OSSService.cs b/IRaCIS.Core.Application/Helper/OSSService.cs index 01a6678f1..061150189 100644 --- a/IRaCIS.Core.Application/Helper/OSSService.cs +++ b/IRaCIS.Core.Application/Helper/OSSService.cs @@ -573,7 +573,15 @@ namespace IRaCIS.Core.Application.Helper - var objects = minioClient.ListObjectsAsync(listArgs).ToListObservable().Select(t => t.Key).ToList(); + // 创建一个空列表用于存储对象键 + var objects = new List(); + + // 使用 await foreach 来异步迭代对象列表 + await foreach (var item in minioClient.ListObjectsEnumAsync(listArgs)) + { + objects.Add(item.Key); + } + if (objects.Count > 0) { @@ -605,37 +613,38 @@ namespace IRaCIS.Core.Application.Helper var amazonS3Client = new AmazonS3Client(credentials, clientConfig); - try + // 列出指定前缀下的所有对象 + var listObjectsRequest = new ListObjectsV2Request { - ListObjectsResponse objectListing = null; - string nextMarker = null; - do + BucketName = awsConfig.BucketName, + Prefix = prefix + }; + + var listObjectsResponse = await amazonS3Client.ListObjectsV2Async(listObjectsRequest); + + if (listObjectsResponse.S3Objects.Count > 0) + { + // 准备删除请求 + var deleteObjectsRequest = new Amazon.S3.Model.DeleteObjectsRequest { - // 使用 prefix 模拟目录结构,设置 MaxKeys 和 NextMarker - objectListing = await amazonS3Client.ListObjectsAsync(new Amazon.S3.Model.ListObjectsRequest() + BucketName = awsConfig.BucketName, + Objects = new List() + }; + + foreach (var s3Object in listObjectsResponse.S3Objects) + { + deleteObjectsRequest.Objects.Add(new KeyVersion { - Prefix = prefix, - MaxKeys = 1000, - Marker = nextMarker + Key = s3Object.Key }); + } - //List keys = objectListing.CommonPrefixes.Select(t => t.Key).ToList(); - - //// 删除获取到的文件 - //if (keys.Count > 0) - //{ - // //await amazonS3Client.DeleteObjectsAsync(new Amazon.S3.Model.DeleteObjectsRequest() { BucketName=awsConfig.BucketName,k}awsConfig.BucketName, keys, false)); - //} - - // 设置 NextMarker 以获取下一页的数据 - nextMarker = objectListing.NextMarker; - - } while (objectListing.IsTruncated); - } - catch (Exception ex) - { - Console.WriteLine($"Error: {ex.Message}"); + // 批量删除对象 + var deleteObjectsResponse = await amazonS3Client.DeleteObjectsAsync(deleteObjectsRequest); } + + + } else {