BusinessException 异常类是自己封装的 根据需要替换
pom依赖:
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>6.0.11</version>
</dependency>
yml配置:
minio: endpoint: http://192.168.8.50:9000 accessKey: admin secretKey: 123123123
配置类 MinioProperties :
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @Data @ConfigurationProperties(prefix = "minio") public class MinioProperties { //连接url private String endpoint; //用户名 private String accessKey; //密码 private String secretKey; }
工具类 MinioUtil
import cn.hutool.core.util.StrUtil; import com.team.common.core.constant.enums.BaseResultEnum; import com.team.common.core.exception.BusinessException; import io.minio.MinioClient; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; @AllArgsConstructor @Component public class MinioUtil { private final MinioClient minioClient; private final MinioProperties minioProperties; /** * http文件上传 * @param bucketName * @param file * @return 访问地址 */ public String putFile(String bucketName,MultipartFile file) { return this.putFile(bucketName,null,file); } /** * http文件上传(增加根路径) * @param bucketName * @param folder * @param file * @return 访问地址 */ public String putFile(String bucketName,String folder,MultipartFile file) { String originalFilename = file.getOriginalFilename(); if (StrUtil.isNotEmpty(folder)){ originalFilename = folder.concat("/").concat(originalFilename); } try { InputStream in = file.getInputStream(); String contentType= file.getContentType(); minioClient.putObject(bucketName,originalFilename,in,null, null, null, contentType); } catch (Exception e) { e.printStackTrace(); throw new BusinessException(BaseResultEnum.SYSTEM_EXCEPTION.getCode(),"文件上传失败"); } String url = minioProperties.getEndpoint().concat("/").concat(bucketName).concat("/").concat(originalFilename); return url; } /** * 创建bucket * @param bucketName */ public void createBucket(String bucketName){ try { minioClient.makeBucket(bucketName); } catch (Exception e) { e.printStackTrace(); throw new BusinessException(BaseResultEnum.SYSTEM_EXCEPTION.getCode(),"创建bucket失败"); } } @SneakyThrows public String getBucketPolicy(String bucketName){ return minioClient.getBucketPolicy(bucketName); } }
装配类:
import io.minio.MinioClient; import io.minio.errors.InvalidEndpointException; import io.minio.errors.InvalidPortException; import lombok.AllArgsConstructor; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @AllArgsConstructor @Configuration @EnableConfigurationProperties(MinioProperties.class) public class MinioAutoConfiguration { private final MinioProperties minioProperties; @Bean public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException { MinioClient client = new MinioClient(minioProperties.getEndpoint(),minioProperties.getAccessKey(),minioProperties.getSecretKey()); return client; } @ConditionalOnBean(MinioClient.class) @Bean public MinioUtil minioUtil(MinioClient minioClient,MinioProperties minioProperties) { return new MinioUtil(minioClient,minioProperties); } }
spring.factories配置文件(不知道这个文件的直接百度 秒懂)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.*(你的路径).MinioAutoConfiguration
demo:
import com.team.common.core.web.Result; import com.team.common.minio.MinioUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @Api(value = "uploadFile", tags = "文件上传") @RequestMapping("uploadFile") @RestController public class UploadFileController { @Autowired private MinioUtil minioUtil; @ApiOperation(value = "通用文件上传") @PutMapping("/upload") public Result uploadFile(@ApiParam("存储桶名称") String bucketName,@ApiParam("文件") MultipartFile file) { String url = null; try { url = minioUtil.putFile(bucketName,file); } catch (Exception e) { e.printStackTrace(); } return Result.success(url); } }