docs.minio.org.cn/docs/master…
MinIO单点模式
docker安装
docker run -d --restart=always -p 9000:9000 -p 9090:9090 --name minio -v /home/docker/minio/data:/data -e "MINIO_ROOT_USER=admin" -e "MINIO_ROOT_PASSWORD=admin123456" minio/minio server /data --console-address ":9090"
springboot集成
pom坐标
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.6</version>
<exclusions>
<exclusion>
<artifactId>kotlin-stdlib</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
</exclusion>
<exclusion>
<artifactId>kotlin-stdlib-common</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.7.22</version>
</dependency>
application.properties
##############################################
# minio配置
##############################################
#Minio服务所在地址
minio.endpoint=http://1.1.1.249:9000
#存储桶名称
minio.bucketName=gjw001
#访问的key
minio.accessKey=gaojunwei
#访问的秘钥
minio.secretKey=admin1234
##############################################
# Spring上传配置
##############################################
spring.servlet.multipart.max-file-size=1024MB
spring.servlet.multipart.max-request-size=1024MB
spring.servlet.multipart.enabled=true
MinioConfig.java
@Setter
@Getter
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
/**
* Minio服务所在地址
*/
private String endpoint;
/**
* 存储桶名称
*/
private String bucketName;
/**
* 访问的key
*/
private String accessKey;
/**
* 访问的秘钥
*/
private String secretKey;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
MinioService.java
public interface MinioService {
/**
* 存储桶是否存在
*/
String uploadFile(MultipartFile file);
/**
* 预览图片
*/
String preview(String fileName);
}
MinioServiceImpl.java
@Slf4j
@Component
public class MinioServiceImpl implements MinioService {
@Resource
private MinioConfig prop;
@Resource
private MinioClient minioClient;
@Override
public String uploadFile(MultipartFile file) {
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
throw new RuntimeException();
}
String fileName = UuidUtils.getUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM/dd")).concat("/").concat(fileName);
try {
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(prop.getBucketName()).object(objectName)
.stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
//文件名称相同会覆盖
minioClient.putObject(objectArgs);
} catch (Exception e) {
logger.error("文件上传失败 errorMsg:{}", e.getMessage(), e);
throw new AppException(SystemCodeEnums.ERROR.getCode(), "文件上传失败");
}
return objectName;
}
@Override
public String preview(String fileName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
// 查看文件地址
GetPresignedObjectUrlArgs build = new GetPresignedObjectUrlArgs().builder().bucket(prop.getBucketName()).object(fileName).method(Method.GET).build();
return minioClient.getPresignedObjectUrl(build);
}
}
CommonController.java
@RestController
@RequestMapping("/")
public class CommonController {
@Resource
private MinioService minioService;
/**
* 上传附件
*/
@PostMapping("/upload")
public SingleResult<String> imgCode(@RequestParam("file") MultipartFile file) throws Exception {
SingleResult<String> result = new SingleResult<>();
result.setCode(SystemCodeEnums.SUCCESS.getCode());
result.setMsg(SystemCodeEnums.SUCCESS.getMsg());
String fileName = minioService.uploadFile(file);
System.out.println("上传后文件名:"+fileName);
String url = minioService.preview(fileName);
result.setData(url);
return result;
}
}