Explorar o código

测试minio定时删除

gao.qiang hai 1 ano
pai
achega
ebf32230b1

+ 1 - 1
hazard-admin/src/main/java/com/ozs/web/controller/accountmanagment/BaseCameraManagementController.java

@@ -969,7 +969,7 @@ public class BaseCameraManagementController extends BaseController {
      */
     @GetMapping("/removePrice")
     public AjaxResult removePrice() {
-        cameraUtil.removePrice();
+        cameraUtil.test();
         return success();
     }
 

+ 60 - 36
hazard-admin/src/main/java/com/ozs/web/core/util/CameraUtil.java

@@ -33,6 +33,7 @@ import io.minio.ListObjectsArgs;
 import io.minio.MinioClient;
 import io.minio.RemoveObjectArgs;
 import io.minio.Result;
+import io.minio.errors.MinioException;
 import io.minio.messages.Item;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -447,54 +448,54 @@ ffmpeg -i "concat:1.ts|2.ts" -c copy output.mp4
         }
         cUtil.cmd("chomd -R 777 " + file.getParentFile().getPath());
         log.info("newfilePath:{}", newfilePath);
-       
+
         StringBuilder argFileTxt = new StringBuilder();
         fromVideoFileList.forEach(filePath -> {
-        	File videoFile = new File(filePath);
-        	if (!videoFile.exists()) {
-        		return;
-        	}
-        	log.info("file:{}", filePath);
+            File videoFile = new File(filePath);
+            if (!videoFile.exists()) {
+                return;
+            }
+            log.info("file:{}", filePath);
 
-        	argFileTxt.append("file '").append(filePath).append("'").append(System.lineSeparator());
+            argFileTxt.append("file '").append(filePath).append("'").append(System.lineSeparator());
         });
-        
+
 //        if (!flay) {
 //        	List<String> validFileList = new ArrayList<>();
 //        	validFileList.add(newfilePath);
 //        	putDeleteCache(validFileList);
 //        }
-        
+
         if (argFileTxt.length() == 0) {
-        	return;
+            return;
         }
-        
+
         String argFileName = IdUtils.fastSimpleUUID() + "_tmp.txt";
         File argFile = new File(file.getParentFile(), argFileName);
         try (
-    		OutputStreamWriter bos = new OutputStreamWriter(new FileOutputStream(argFile), StandardCharsets.UTF_8);
+                OutputStreamWriter bos = new OutputStreamWriter(new FileOutputStream(argFile), StandardCharsets.UTF_8);
         ) {
-        	bos.write(argFileTxt.toString());
-        	bos.flush();
+            bos.write(argFileTxt.toString());
+            bos.flush();
         }
-        
+
         StringBuilder cmd = new StringBuilder(ffmpegPath);
         cmd.append(" -f concat -safe 0 -y -i ")
-        		.append(argFile.getAbsolutePath())
-	        	.append(" -c copy ")
-	        	.append(newfilePath);
-        
+                .append(argFile.getAbsolutePath())
+                .append(" -c copy ")
+                .append(newfilePath);
+
         log.info("合并命令:{}", cmd);
-        
+
         cUtil.cmd(cmd.toString());
-        
+
         argFile.delete();
     }
-    
+
     private static void putDeleteCache(List<String> pathList) {
-    	if (ObjectUtils.isEmpty(pathList)) {
-    		return;
-    	}
+        if (ObjectUtils.isEmpty(pathList)) {
+            return;
+        }
         Map<String, Object> mergeVideoTsFile = rc.getCacheMap(tsFilekey);
         if (ObjectUtils.isEmpty(mergeVideoTsFile)) {
             mergeVideoTsFile = new HashMap<>();
@@ -1035,7 +1036,7 @@ ffmpeg -i "concat:1.ts|2.ts" -c copy output.mp4
     }
 
 
-    public void removePrice(){
+    public void removePrice() {
         try {
             // 创建MinioClient对象
             MinioClient minioClient = MinioClient.builder()
@@ -1057,10 +1058,10 @@ 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);
+    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)
@@ -1068,13 +1069,13 @@ ffmpeg -i "concat:1.ts|2.ts" -c copy output.mp4
                 .build());
 
         boolean hasFiles = false;
-        log.info("----results----"+results.toString());
+        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());
+            log.info("----item.lastModified()----" + item.lastModified());
+            log.info("----NoDeleted----" + item.objectName());
             if (!item.isDir() && item.lastModified().isBefore(deleteTime)) {
                 minioClient.removeObject(RemoveObjectArgs.builder()
                         .bucket(bucketName)
@@ -1100,6 +1101,29 @@ ffmpeg -i "concat:1.ts|2.ts" -c copy output.mp4
         }
     }
 
+
+    public void test() {
+        try {
+            // Initialize Minio client
+            MinioClient minioClient = MinioClient.builder()
+                    .endpoint(endpoint)
+                    .credentials(accessKey, secretKey)
+                    .build();
+
+            // Bucket name
+            String bucketName = this.bucketName;
+
+            // List all objects in the bucket
+            Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
+            for (Result<Item> result : results) {
+                Item item = result.get();
+                log.info(item.objectName());
+            }
+        } catch (Exception e) {
+            log.error("Error occurred: " + e);
+        }
+    }
+
     public static void main(String[] args) {
 
 //            // 创建一个集合
@@ -1126,10 +1150,10 @@ ffmpeg -i "concat:1.ts|2.ts" -c copy output.mp4
 //        System.out.println(new Date(time2));
         // 视频拼接
         String streamPath = "42010001541320000023/42010001541320000023";
-        String date="2024-03-11";
+        String date = "2024-03-11";
         String param = "{\n" +
                 "    \"streamPath\":\"" + streamPath + "\",\n" +
-                "    \"date\":\""+date+"\"\n" +
+                "    \"date\":\"" + date + "\"\n" +
                 "}";
 
         String jsonStr = HttpUtils.sendPost("http://124.70.58.209:9080" + "/recordpro/api/query/records", param);
@@ -1138,6 +1162,6 @@ ffmpeg -i "concat:1.ts|2.ts" -c copy output.mp4
         JSONObject resultObj = resultArray.getJSONObject(0);
         String path = resultObj.getString("Path");
         System.out.println("Path: " + path);
-      
+
     }
 }