Minio uses detailed

Following Minio environment to build articles, tidy minio back end use articles, the main steps are as follows:

  • 1. Introducing a Jar package, gradle manner:
compile 'io.minio: minium: 5.0.2'
  • Writing service bean
     1 @Service
     2 @Slf4j
     3 public class MinioService {
     4 
     5     private MinioClient minioClient;
     6 
     7     private MimetypesFileTypeMap mimetypesFileTypeMap;
     8 
     9   
    10     @Value("${minio.address}")
    11     private String address;
    12 
    13     @Value("${minio.accessKey}")
    14     private String accessKey;
    15 
    16     @Value("${minio.secretKey}")
    17     private String secretKey;
    18 
    19     public String getContentType(String fileName) {
    20         if (mimetypesFileTypeMap == null) {
    21             mimetypesFileTypeMap = new MimetypesFileTypeMap();
    22         }
    23         return mimetypesFileTypeMap.getContentType(fileName);
    24     }
    25     @PostConstruct
    26     private void init() throws Exception {
    27         minioClient = new MinioClient(address, accessKey, secretKey);
    28     }
    29 
    30     public MinioClient getMinioClient() {
    31         return this.minioClient;
    32     }
    33     public String getContentType(String fileName) {
    34         if (mimetypesFileTypeMap == null) {
    35             mimetypesFileTypeMap = new MimetypesFileTypeMap();
    36         }
    37         return mimetypesFileTypeMap.getContentType(fileName);
    38     }
    39 
    40     public MinioFile getBucketFromUrl(String url) {
    41         String[] split = url.split("/");
    42         if (split.length < 3) {
    43             throw new RuntimeException("getBucketFromUrl: url " + url + " format is incorrect");
    44         }
    45         StringBuilder stringBuilder = new StringBuilder();
    46         for (int i = 2; i < split.length; i++) {
    47             stringBuilder.append(split[i]);
    48             if (i != split.length - 1) {
    49                 stringBuilder.append("/");
    50             }
    51         }
    52         String fileName = stringBuilder.toString();
    53         return new MinioFile(split[1], fileName);
    54     }
    55 
    56 }
    View Code

     

  • Write file upload service
    public class UploadService {
    
        @Autowired
        MinioService minioService;
    
        public String uploadFile(MultipartFile file, String type) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidArgumentException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, ParameterException {
            String fileUrl;
            MinioClient minioClient = minioService.getMinioClient();
            byte[] fileContent = file.getBytes();
            byte[] encodeBase64 = org.apache.commons.codec.binary.Base64.encodeBase64(fileContent);
            String base64Encode = new String(encodeBase64, "UTF-8");
    
            String originalFilename = file.getOriginalFilename();
            String contentType = minioService.getContentType(originalFilename);
            String suffix = UploadUtil.getFileSuffix(originalFilename);
    
            String uuid = CommonUtil.getUUID();
            if (!UploadUtil.isFileTypeSupport(suffix)){
                throw new ParameterException("file type is not support");
            }
            String currentDay = CommonUtil.getCurrentDayStr();
    
            String fileName = type +"/" +currentDay + "/" + uuid + "." + suffix;
    
            log.info("before upload------------------");
    
            InputStream inputStream = new ByteArrayInputStream(base64Encode.getBytes());
            minioClient.putObject(MinioType.BUCKET_RIO, fileName, inputStream, contentType);
            log.info("uploaded----------------");
            fileUrl = "/" + MinioType.BUCKET_RIO + "/" + fileName;
            return fileUrl;
        }
    
    
        public Response downloadFile(DownLoadBean bean){
            MinioClient minioClient = minioService.getMinioClient();
            InputStream in = null;
            String fileStr =null;
            String objectName = "";
            try {
                String[] minioInfo = bean.getFileUrl().split("/", -1);
                if(minioInfo.length==4) {
                    objectName = minioInfo[2] + "/" + minioInfo[3];
                }else{
                    objectName = minioInfo[2] + "/" + minioInfo[3]+ "/" + minioInfo[4];
                }
                in = minioClient.getObject(minioInfo[1], objectName);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int i = 0;
                while ((i = in.read()) != -1) {
                    out.write(i);
                }
                fileStr = out.toString();
            }catch (Exception e){
                log.error(e.getMessage(),e);
            }finally {
                CloseableUtils.closeQuietly(in);
            }
            return Response.success(fileStr);
        }
    
    }
    View Code

     

  • If you need to upload into a temporary table, using this class, you can catch tracking data minio server, if it is dirty, you can use the following regular tasks to clean up
    @Service
    @Slf4j
    @Transactional(rollbackFor = Exception.class)
    public class PicTempService {
    
        @Autowired
        MinioService minioService;
    
        @Autowired
        private PicTempDao tempDao;
    
        public String uploadPic(MultipartFile file,  String type) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidArgumentException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, ParameterException {
            String imageUrl;
            String uuid = CommonUtil.getUUID();
            MinioClient minioClient = minioService.getMinioClient();
            InputStream is = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String contentType = minioService.getContentType(originalFilename);
            String suffix = UploadUtil.getFileSuffix(originalFilename);
            if (!UploadUtil.isPicTypeSupport(suffix)){
                throw new ParameterException("pic type is not support");
            }
            String currentDay = TimeUtils.getCurrentDayStr();
    
            String fileName = currentDay + "/" + uuid + "." + suffix;
            minioClient.putObject(type, fileName, is, contentType);
            imageUrl = "/" + type + "/" + fileName;
            PicParams param = new PicParams();
            param.setUrl(imageUrl);
            param.setUuid(uuid);
            tempDao.insertPicTmp(param);
            return uuid;
        }
    
        public void initUrlAndDelTempPic(BasePicContent pic) throws Exception{
            try {
                PicTemp temp = tempDao.getPicByUuid(pic.getUuid());
                pic.setUrl (temp.getUrl ()); 
                tempDao.deleteTempPicByUuid (pic.getUuid ()); 
            } the catch (Exception E) { 
                log.error (e.getMessage (), E); 
                the throw  new new ParameterException ( "image parameters mistake "! ); 
            } 
        } 
    
    
    
    }
    View Code

     

  • Timing task to delete temporary Minio dirty data
    @Component
    @Slf4j
    public class TempFileScheduler {
    
        @Autowired
        private PicTempDao tempDao;
    
        @Autowired
        private MinioService minioService;
    
        @Scheduled(cron = "0 0/30 * * * ?")
        public void deleteTempPic() {
            try {
                log.info("@################ start job  deleteTempPic #################");
                List<PicTemp> picTempList = tempDao.getTmpPicDaysBefore("3", TimeUtils.DAY);
                MinioClient minioClient = minioService.getMinioClient();
                for (PicTemp picTemp : picTempList) {
                    tempDao.deletePicTempById(picTemp.getId());
                    MinioFile minioFile = minioService.getBucketFromUrl(picTemp.getUrl());
                    minioClient.removeObject(minioFile.getBucket(), minioFile.getFileName());
                }
                log.info("@################ end job deleteTempPic #################");
            } catch (Exception e) {
                log.error("deleteTempPic", e);
            }
        }
    }
    View Code

     

 

Guess you like

Origin www.cnblogs.com/xifenglou/p/12191139.html