| | |
| | | package com.ruoyi.quartz.task; |
| | | |
| | | import com.ruoyi.device.hiksdk.util.minio.MinioUtils; |
| | | import com.ruoyi.utils.minio.MinioUtil; |
| | | import io.minio.Result; |
| | | import io.minio.messages.Item; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.ZoneId; |
| | | import java.time.format.DateTimeFormatter; |
| | |
| | | public class MinioTask { |
| | | |
| | | public void delHistoryTask(Integer saveDays) { |
| | | log.info("执行历史数据删除"); |
| | | log.debug("执行历史数据删除"); |
| | | //遍历存储 |
| | | try { |
| | | List<String> buckets=new ArrayList<>(); |
| | | List<String> buckets = new ArrayList<>(); |
| | | buckets.add("pic"); |
| | | buckets.add("record"); |
| | | for (String name:buckets) { |
| | | Iterable<Result<Item>> listObjectsPic = MinioUtils.getObjectsByBucket(name); |
| | | for (String name : buckets) { |
| | | Iterable<Result<Item>> listObjectsPic = MinioUtil.getObjectsByBucket(name); |
| | | for (Result<Item> result : listObjectsPic) { |
| | | Item item = result.get(); |
| | | log.info(item.objectName() + "创建时间:" + item.lastModified()); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai")); |
| | | String objectTime = item.lastModified().format(formatter); |
| | | |
| | | Date now= new Date(); |
| | | Date now = new Date(); |
| | | // 把日期往前增加一天,整数往后推,负数往前移动 |
| | | Calendar calendar = new GregorianCalendar(); |
| | | calendar.setTime(now); |
| | | calendar.add(Calendar.DATE, saveDays); |
| | | SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String nowTime = fmt.format(calendar.getTime()); |
| | | String expireTime = fmt.format(calendar.getTime()); |
| | | |
| | | log.info("objectTime : " + objectTime); |
| | | log.info("nowTime : " + nowTime); |
| | | |
| | | if (objectTime.compareTo(nowTime) < 0) { |
| | | log.info("objectTime 时间在 nowTime 之前"); |
| | | boolean b = MinioUtils.removeObject(name, item.objectName()); |
| | | if(b) |
| | | { |
| | | log.info("删除过期数据:"+ item.objectName()+"---"+item.lastModified()); |
| | | if (objectTime.compareTo(expireTime) < 0) { |
| | | log.debug("objectTime : " + objectTime); |
| | | log.debug("expireTime : " + expireTime); |
| | | log.debug("objectTime 时间在 expireTime 之前,执行删除"); |
| | | boolean b = MinioUtil.removeObject(name, item.objectName()); |
| | | if (b) { |
| | | log.debug("成功删除过期数据:" + item.objectName() + "---" + item.lastModified()); |
| | | } |
| | | } |
| | | } |
| | |
| | | log.error("minio历史数据删除异常:" + ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | } |