Minio使用详解
Posted xifenglou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Minio使用详解相关的知识,希望对你有一定的参考价值。
继Minio环境搭建篇之后,整理一下minio后端使用篇,主要步骤如下:
- 1.引入Jar包,gradle方式:
compile ‘io.minio:minio:5.0.2‘
- 编写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 }
- 编写文件上传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); } }
- 如果需要上传存入临时表,使用该类,可以追追踪minio server的数据,如果是脏数据,可以使用下面的定时任务进行清理
@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()); }catch (Exception e){ log.error(e.getMessage(),e); throw new ParameterException("图片参数有误!"); } } }
- 定时任务删除临时Minio脏数据
@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); } } }
以上是关于Minio使用详解的主要内容,如果未能解决你的问题,请参考以下文章
14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段