删除OSS脏文件夹

This commit is contained in:
he
2025-01-20 14:24:49 +08:00
parent 146b0635c8
commit a9a034923f
3 changed files with 83 additions and 1 deletions
@@ -145,6 +145,8 @@ public interface IOSSService
public Task DeleteFromPrefix(string prefix);
List<string> GetRootFolderNames();
public ObjectStoreDTO GetObjectStoreTempToken();
}
@@ -526,6 +528,55 @@ public class OSSService : IOSSService
}
/// <summary>
/// 获取所有根目录名称
/// </summary>
/// <returns></returns>
public List<string> GetRootFolderNames()
{
GetObjectStoreTempToken();
var aliConfig = ObjectStoreServiceOptions.AliyunOSS;
var _ossClient = new OssClient(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? aliConfig.EndPoint : aliConfig.InternalEndpoint,
AliyunOSSTempToken.AccessKeyId,
AliyunOSSTempToken.AccessKeySecret,
AliyunOSSTempToken.SecurityToken);
List<string> rootFolders = new List<string>();
string nextMarker = null;
try
{
ObjectListing objectListing = null;
do
{
// 列出根目录下的对象和文件夹
objectListing = _ossClient.ListObjects(new Aliyun.OSS.ListObjectsRequest(aliConfig.BucketName)
{
MaxKeys = 1000,
Marker = nextMarker,
Delimiter = "/" // 使用分隔符来模拟文件夹
});
// 遍历 CommonPrefixes 获取根文件夹名称
foreach (var prefix in objectListing.CommonPrefixes)
{
rootFolders.Add(prefix.TrimEnd('/')); // 去掉末尾的斜杠
}
// 设置 NextMarker 以获取下一页的数据
nextMarker = objectListing.NextMarker;
} while (objectListing.IsTruncated);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
return rootFolders;
}
/// <summary>
/// 删除某个目录的文件
/// </summary>