修改s3 minio 删除方法
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
3d304961d3
commit
6cc4c32625
|
@ -379,7 +379,7 @@ namespace IRaCIS.Api.Controllers
|
||||||
SecretAccessKey = credentials.SecretAccessKey,
|
SecretAccessKey = credentials.SecretAccessKey,
|
||||||
SessionToken= credentials.SessionToken,
|
SessionToken= credentials.SessionToken,
|
||||||
Expiration=credentials.Expiration,
|
Expiration=credentials.Expiration,
|
||||||
|
Region=awsOptions.Region,
|
||||||
|
|
||||||
BucketName = awsOptions.BucketName,
|
BucketName = awsOptions.BucketName,
|
||||||
EndPoint = awsOptions.EndPoint,
|
EndPoint = awsOptions.EndPoint,
|
||||||
|
@ -422,7 +422,7 @@ namespace IRaCIS.Api.Controllers
|
||||||
SecretAccessKey = credentials.SecretAccessKey,
|
SecretAccessKey = credentials.SecretAccessKey,
|
||||||
SessionToken = credentials.SessionToken,
|
SessionToken = credentials.SessionToken,
|
||||||
Expiration = credentials.Expiration,
|
Expiration = credentials.Expiration,
|
||||||
|
Region = awsOptions.Region,
|
||||||
BucketName = awsOptions.BucketName,
|
BucketName = awsOptions.BucketName,
|
||||||
EndPoint = awsOptions.EndPoint,
|
EndPoint = awsOptions.EndPoint,
|
||||||
ViewEndpoint = awsOptions.ViewEndpoint,
|
ViewEndpoint = awsOptions.ViewEndpoint,
|
||||||
|
|
|
@ -573,7 +573,15 @@ namespace IRaCIS.Core.Application.Helper
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var objects = minioClient.ListObjectsAsync(listArgs).ToListObservable().Select(t => t.Key).ToList();
|
// 创建一个空列表用于存储对象键
|
||||||
|
var objects = new List<string>();
|
||||||
|
|
||||||
|
// 使用 await foreach 来异步迭代对象列表
|
||||||
|
await foreach (var item in minioClient.ListObjectsEnumAsync(listArgs))
|
||||||
|
{
|
||||||
|
objects.Add(item.Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (objects.Count > 0)
|
if (objects.Count > 0)
|
||||||
{
|
{
|
||||||
|
@ -605,37 +613,38 @@ namespace IRaCIS.Core.Application.Helper
|
||||||
|
|
||||||
var amazonS3Client = new AmazonS3Client(credentials, clientConfig);
|
var amazonS3Client = new AmazonS3Client(credentials, clientConfig);
|
||||||
|
|
||||||
try
|
// 列出指定前缀下的所有对象
|
||||||
|
var listObjectsRequest = new ListObjectsV2Request
|
||||||
{
|
{
|
||||||
ListObjectsResponse objectListing = null;
|
BucketName = awsConfig.BucketName,
|
||||||
string nextMarker = null;
|
Prefix = prefix
|
||||||
do
|
};
|
||||||
|
|
||||||
|
var listObjectsResponse = await amazonS3Client.ListObjectsV2Async(listObjectsRequest);
|
||||||
|
|
||||||
|
if (listObjectsResponse.S3Objects.Count > 0)
|
||||||
{
|
{
|
||||||
// 使用 prefix 模拟目录结构,设置 MaxKeys 和 NextMarker
|
// 准备删除请求
|
||||||
objectListing = await amazonS3Client.ListObjectsAsync(new Amazon.S3.Model.ListObjectsRequest()
|
var deleteObjectsRequest = new Amazon.S3.Model.DeleteObjectsRequest
|
||||||
{
|
{
|
||||||
Prefix = prefix,
|
BucketName = awsConfig.BucketName,
|
||||||
MaxKeys = 1000,
|
Objects = new List<KeyVersion>()
|
||||||
Marker = nextMarker
|
};
|
||||||
|
|
||||||
|
foreach (var s3Object in listObjectsResponse.S3Objects)
|
||||||
|
{
|
||||||
|
deleteObjectsRequest.Objects.Add(new KeyVersion
|
||||||
|
{
|
||||||
|
Key = s3Object.Key
|
||||||
});
|
});
|
||||||
|
|
||||||
//List<string> 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
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue