Java集成Minio实现上传文件
前言此接口基于minio实现上传前会先校验名称是否重复重复会生成一个随机的UUID拼接到文件名末尾。一、依赖添加dependency groupIdio.minio/groupId artifactIdminio/artifactId version8.5.6/version /dependency二、代码实现1. controllerimport com.cloud.zhenyu.pojo.CommonResult; import com.cloud.zhenyu.service.FileService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; Tag(name 文件管理接口, description 文件上传、下载、MinIO操作) RestController RequestMapping(/file) Validated public class FileController { Resource private FileService fileService; PostMapping(/upload) Operation(summary 上传文件) public CommonResultString uploadFile(RequestParam(file) MultipartFile file) throws Exception { return fileService.uploadFile(file); } }2. serviceimport com.cloud.zhenyu.pojo.CommonResult; import org.springframework.web.multipart.MultipartFile; public interface FileService { /** * 上传文件 * * param file * return */ CommonResultString uploadFile(MultipartFile file); }3. serviceImplimport com.cloud.zhenyu.pojo.CommonResult; import com.cloud.zhenyu.service.FileService; import io.minio.MinioClient; import io.minio.PutObjectArgs; import io.minio.StatObjectArgs; import io.minio.errors.MinioException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; import java.util.UUID; Service public class FileServiceImpl implements FileService { // MinIO 服务地址 private static final String MINIO_ENDPOINT ip:port; // AccessKey 页面生成 private static final String ACCESS_KEY yours; // SecretKey 页面生成 private static final String SECRET_KEY yours; // 自定义存储桶自行修改桶名桶需提前在MinIO创建 private static final String BUCKET_NAME bucket_name; private static final Logger log LoggerFactory.getLogger(FileServiceImpl.class); Override public CommonResultString uploadFile(MultipartFile file) { // 1. 校验文件 if (file null || file.isEmpty ()) { return CommonResult.error(500, 文件不能为空); } // 2. 创建 MinIO 客户端 MinioClient minioClient MinioClient.builder () .endpoint (MINIO_ENDPOINT) .credentials (ACCESS_KEY, SECRET_KEY) .build (); // 3. 原始文件名 String originalFilename file.getOriginalFilename (); String objectName originalFilename; try { // 判断文件是否已存在 minioClient.statObject( StatObjectArgs.builder() .bucket(BUCKET_NAME) .object(originalFilename) .build() ); // 走到这里说明文件重名截取UUID前10位拼接在文件名后 String shortUuid UUID.randomUUID().toString().substring(0,10); // 分割文件名与后缀 int dotIndex originalFilename.lastIndexOf(.); if (dotIndex ! -1) { String name originalFilename.substring(0, dotIndex); String suffix originalFilename.substring(dotIndex); objectName name _ shortUuid suffix; } else { // 无后缀文件 objectName originalFilename _ shortUuid; } } catch (MinioException e) { // 文件不存在会抛异常直接使用原文件名 } catch (Exception e) { log.info(文件上传失败 e.getMessage()); } // 4. 获取文件输入流 try (InputStream inputStream file.getInputStream ()) { // 5. 上传文件到桶 minioClient.putObject ( PutObjectArgs.builder () .bucket (BUCKET_NAME) .object (objectName) .stream (inputStream, file.getSize (), -1) .contentType (file.getContentType ()) .build () ); } catch (Exception e) { log.info(文件上传失败 e.getMessage()); } // 6. 拼接文件可访问 URL String fileUrl MINIO_ENDPOINT / BUCKET_NAME / objectName; return CommonResult.success(fileUrl); } }