Minio使用详解
继Minio环境搭建篇之后,
具体链接: https://www.cnblogs.com/xifenglou/p/11988338.html
整理一下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); } } }
------------------你若盛开 - 蝴蝶自来------------------
如果,您认为阅读这篇博客让您有些收获, 如果,您希望更容易地发现我的新博客,不妨关注一下。因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客。
因为有小孩,兼职卖书,路过的朋友有需要低价购买图书、点读笔、纸尿裤等资源的,可扫最上方二维码,质量有保证,价格很美丽,欢迎咨询! 欢迎关注微信公众号:18般武艺
如果,您认为阅读这篇博客让您有些收获, 如果,您希望更容易地发现我的新博客,不妨关注一下。因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客。
因为有小孩,兼职卖书,路过的朋友有需要低价购买图书、点读笔、纸尿裤等资源的,可扫最上方二维码,质量有保证,价格很美丽,欢迎咨询! 欢迎关注微信公众号:18般武艺