|
@@ -10,6 +10,8 @@ import java.io.InputStreamReader;
|
|
|
import java.io.OutputStreamWriter;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.ZonedDateTime;
|
|
@@ -33,7 +35,13 @@ import io.minio.ListObjectsArgs;
|
|
|
import io.minio.MinioClient;
|
|
|
import io.minio.RemoveObjectArgs;
|
|
|
import io.minio.Result;
|
|
|
+import io.minio.errors.ErrorResponseException;
|
|
|
+import io.minio.errors.InsufficientDataException;
|
|
|
+import io.minio.errors.InternalException;
|
|
|
+import io.minio.errors.InvalidResponseException;
|
|
|
import io.minio.errors.MinioException;
|
|
|
+import io.minio.errors.ServerException;
|
|
|
+import io.minio.errors.XmlParserException;
|
|
|
import io.minio.messages.Item;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -1059,48 +1067,50 @@ ffmpeg -i "concat:1.ts|2.ts" -c copy output.mp4
|
|
|
}
|
|
|
|
|
|
private static void deleteObjectsAndEmptyDirectoriesRecursively(MinioClient minioClient, String bucketName, String prefix, ZonedDateTime deleteTime) throws Exception {
|
|
|
- log.info("----bucketName----" + bucketName);
|
|
|
- log.info("----prefix----" + prefix);
|
|
|
- log.info("----deleteTime----" + deleteTime);
|
|
|
Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder()
|
|
|
.bucket(bucketName)
|
|
|
.prefix(prefix)
|
|
|
.recursive(true)
|
|
|
.build());
|
|
|
|
|
|
- boolean hasFiles = false;
|
|
|
- log.info("----results----" + results.toString());
|
|
|
for (Result<Item> result : results) {
|
|
|
Item item = result.get();
|
|
|
-
|
|
|
- // 如果是文件且在指定时间之前,则删除文件
|
|
|
- log.info("----item.lastModified()----" + item.lastModified());
|
|
|
- log.info("----NoDeleted----" + item.objectName());
|
|
|
- if (!item.isDir() && item.lastModified().isBefore(deleteTime)) {
|
|
|
- minioClient.removeObject(RemoveObjectArgs.builder()
|
|
|
- .bucket(bucketName)
|
|
|
- .object(item.objectName())
|
|
|
- .build());
|
|
|
- log.info("Deleted: " + item.objectName());
|
|
|
- hasFiles = true;
|
|
|
+ ZonedDateTime itemLastModified = item.lastModified();
|
|
|
+
|
|
|
+ // 检查文件夹的上次修改时间是否在删除时间之前
|
|
|
+ if (item.isDir() && itemLastModified.isBefore(deleteTime)) {
|
|
|
+ // 获取下一级目录的时间
|
|
|
+ ZonedDateTime nextLevelLastModified = getNextLevelDirectoryLastModified(minioClient, bucketName, item.objectName());
|
|
|
+
|
|
|
+ // 如果下一级目录的时间早于删除时间,则删除当前目录
|
|
|
+ if (nextLevelLastModified != null && nextLevelLastModified.isBefore(deleteTime)) {
|
|
|
+ minioClient.removeObject(RemoveObjectArgs.builder()
|
|
|
+ .bucket(bucketName)
|
|
|
+ .object(item.objectName())
|
|
|
+ .build());
|
|
|
+ log.info("删除文件夹: " + item.objectName());
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static ZonedDateTime getNextLevelDirectoryLastModified(MinioClient minioClient, String bucketName, String prefix) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
|
|
|
+ Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder()
|
|
|
+ .bucket(bucketName)
|
|
|
+ .prefix(prefix)
|
|
|
+ .recursive(false) // 只获取当前目录下的内容
|
|
|
+ .build());
|
|
|
|
|
|
- // 如果是目录,则递归删除目录下的文件和空目录
|
|
|
+ for (Result<Item> result : results) {
|
|
|
+ Item item = result.get();
|
|
|
if (item.isDir()) {
|
|
|
- deleteObjectsAndEmptyDirectoriesRecursively(minioClient, bucketName, item.objectName(), deleteTime);
|
|
|
+ return item.lastModified();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 如果目录下没有文件且不是根目录,则删除该目录
|
|
|
- if (!hasFiles && !prefix.isEmpty()) {
|
|
|
- minioClient.removeObject(RemoveObjectArgs.builder()
|
|
|
- .bucket(bucketName)
|
|
|
- .object(prefix)
|
|
|
- .build());
|
|
|
- log.info("Deleted directory: " + prefix);
|
|
|
- }
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public void test() {
|
|
|
try {
|