# minio基础分装 **Repository Path**: 124327288/minio-basic-packaging ## Basic Information - **Project Name**: minio基础分装 - **Description**: minio基础分装 ## MinioConfig minio配置,基本配置包括 ip、port、accessKey、secretKey、bucketName、secure minio服务的ip、端口、访问密钥、密钥、桶名称、是否https请求 ## MinioService 封装的以下minio操作方法 - **Primary Language**: Java - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-12-09 - **Last Updated**: 2023-12-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # minio基础分装 ## MinioConfig minio配置,基本配置包括 ip、port、accessKey、secretKey、bucketName、secure minio服务的ip、端口、访问密钥、密钥、桶名称、是否https请求 ## MinioService 封装的以下minio操作方法 bucketExists查看存储bucket是否存在 makeBucket创建存储bucket removeBucket删除存储bucket getAllBuckets获取全部bucket upload文件上传 uploadLocalFile(String bucketName, String localFile, String objectName) 上传本地文件,指定本地文件及存储文件名 preview根据指定文件和桶名预览 download文件下载 open文件内嵌显示 downloadSlice文件分片下载 downloadToLocal文件下载到本地 downloadStream下载文件流 downloadAndCompressFiles批量下载同一个桶内文件并zip压缩到指定地址 listObjects查看桶里文件对象 remove删除文件对象 ## SnowFlake 雪花算法 常用controller操作 @Api(description = "文件上传minio") @RestController public class MinioController { private final Logger logger = LoggerFactory.getLogger(MinioController.class); @Autowired private MinioService minioService; @ApiOperation(value = "批量文件上传接口", notes = "批量文件上传接口", produces = "application/json") @ApiImplicitParam(name = "fileName", value = "多个文件", paramType = "query", required = true, dataType = "File") @RequestMapping(value = "batchFileUpload", method = RequestMethod.POST) @ResponseBody public String batchFileUpload(@RequestParam("fileName") MultipartFile[] files) { StringBuilder sb = new StringBuilder(); if (null != files && files.length > 0) { for (MultipartFile file : files) { sb.append(this.fileUpload(file)).append(","); } sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } /** * @return java.lang.String * @Description 实现文件上传 * @author wangTao * @date 2020/7/13 2020/7/13 */ @ApiOperation(value = "文件上传接口", notes = "文件上传接口", produces = "application/json") @ApiImplicitParam(name = "fileName", value = "单个文件", paramType = "query", required = true, dataType = "File") @RequestMapping(value = "fileUpload", method = RequestMethod.POST) @ResponseBody public String fileUpload(@RequestParam("fileName") MultipartFile file) { if (file == null && file.isEmpty()) { logger.error("{}文件上传失败:{}", this.getClass().getSimpleName(), "参数错误"); return "false"; } try { String fileName = minioService.upload(file); return fileName; } catch (Exception e) { logger.error("{}文件上传失败:{}", this.getClass().getSimpleName(), e.getMessage()); return "false"; } } @ApiOperation(value = "获取文件接口", notes = "获取文件接口", produces = "application/json") @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "文件名,从(文件上传接口)获得的返回", paramType = "query", required = true, dataType = "String"), @ApiImplicitParam(name = "cacheTime", value = "缓存时间,单位s", paramType = "query", required = false, dataType = "int")}) @RequestMapping(value = "get", method = RequestMethod.GET) @ResponseBody public void getResource(@RequestParam("file") String file,HttpServletResponse response,@RequestParam(required = false)Integer cacheTime) { if (StringUtils.isNotBlank(file)) { try { if (cacheTime!=null&& cacheTime>0) { // 设置缓存控制 设置缓存有效期为 秒 CacheControl cacheControl = CacheControl.maxAge(Integer.valueOf(cacheTime), TimeUnit.SECONDS); response.setHeader("Cache-Control", cacheControl.getHeaderValue()); } minioService.download(file, response); } catch (Exception e) { logger.error("{}获取文件失败:{}", this.getClass().getSimpleName(), e.getMessage()); } } } /** * 分片下载,内嵌显示文件,支持ios视频播放 * @param file * @param response */ @ApiOperation(value = "分片下载文件接口", notes = "分片下载文件接口", produces = "application/json") @ApiImplicitParam(name = "file", value = "文件名,从(文件上传接口)获得的返回", paramType = "query", required = true, dataType = "String") @RequestMapping(value = "downloadSlice", method = RequestMethod.GET) public void downloadSlice(@RequestParam("file") String file, HttpServletResponse response, HttpServletRequest request) { if (StringUtils.isNotBlank(file)) { try { minioService.downloadSlice(file, response,request); } catch (Exception e) { logger.error("{}获取文件失败:{}", this.getClass().getSimpleName(), e.getMessage()); } } } @ApiOperation(value = "创建桶", notes = "创建bucket", produces = "application/json") @ApiImplicitParam(name = "bucketName", value = "桶名称", paramType = "query", required = true, dataType = "String") @RequestMapping(value = "createBucket", method = RequestMethod.POST) public Boolean createBucket(@RequestParam("bucketName") String bucketName) { if (StringUtils.isNotBlank(bucketName)) { try { return minioService.makeBucket(bucketName); } catch (Exception e) { logger.error("{}创建桶失败:{}", this.getClass().getSimpleName(), e.getMessage()); } } return false; } /** * 批量下载文件接口 * @param file * @param response */ @ApiOperation(value = "批量下载文件接口", notes = "批量下载文件接口", produces = "application/json") @ApiImplicitParam(name = "file", value = "文件名,从(文件上传接口)获得的返回", paramType = "query", required = true, dataType = "String") @RequestMapping(value = "downloadBatch", method = RequestMethod.GET) public void downloadBatch(@RequestParam("file") List file, HttpServletResponse response) { if (!CollectionUtils.isEmpty(file)) { File compressedFile = null; try { // 设置响应头 response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); String fileName = SnowFlake.generateId()+ ".zip"; response.setHeader("Content-Disposition", "attachment;filename="+fileName); //获取压缩文件 String compressFiles = minioService.downloadAndCompressFiles(file); // 读取并写入响应体 compressedFile = new File(compressFiles); FileInputStream fileInputStream = new FileInputStream(compressedFile); FileCopyUtils.copy(fileInputStream, response.getOutputStream()); fileInputStream.close(); } catch (Exception e) { logger.error("{}批量下载错误:{}", this.getClass().getSimpleName(), e.getMessage()); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }finally { // 删除临时下载的文件 if (compressedFile != null) { compressedFile.delete(); } } } } }