1.集成jar包
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.yml配置
minio:
endpoint: http://127.0.0.1:9000
accessKey: minioadmin
secretKey: minioadmin
bucketName: yourProjectBuckteName
3.MinioProperties 配置
package com.qfedu.springbootminio.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* minio属性类
*/
@Data
// 通过指定的前缀,绑定配置文件中的配置
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
/**
* 端点
*/
private String endpoint;
private String accessKey;
private String secretKey;
/**
* 桶名称
*/
private String bucketName;
}
4.MinioConfig 配置
package com.qfedu.springbootminio.config;
import io.minio.MinioClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* minio配置类
*/
@Component
// 使@ConfigurationProperties注解修饰的类生效,
// 或者不使用该注解,但是在@ConfigurationProperties注解修饰的类上使用@Component
@EnableConfigurationProperties(MinioProperties.class)
public class MinioConfig {
@Resource
private MinioProperties minioProperties;
/**
* 创建 MinIO 客户端
*/
@Bean
public MinioClient getMinioClient() {
MinioClient minioClient = MinioClient.builder().endpoint(minioProperties.getEndpoint())
.credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
.build();
return minioClient;
}
}
5. MinioUtil 工具类
package com.qfedu.springbootminio.config;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
/**
* minio工具类
*/
@Component
public class MinioUtil {
@Value("${minio.bucketName}")
private String bucketName;
@Autowired
private MinioClient minioClient;
/**
* 判断bucket是否存在,不存在则创建
*/
public void existBucket(String name) {
try {
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());
if (!exists) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
*
* @param file
* @return
*/
public String upload(MultipartFile file) {
// 如果桶不存在,直接创建
existBucket(bucketName);
// 获取原始文件名
String fileName = file.getOriginalFilename();
// 修改文件名,按照指定的规则修改,本例增加了一个时间戳
String[] split = fileName.split("\\.");
if (split.length > 1) {
fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
} else {
fileName = fileName + System.currentTimeMillis();
}
InputStream in = null;
try {
in = file.getInputStream();
// 上传到minio
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName) //.bucket("img/" + bucketName)
.object(fileName)
.stream(in, in.available(), -1)
.contentType(file.getContentType())
.build()
);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 返回上传的文件名
return fileName;
}
}
6.TestController 控制层
package com.qfedu.springbootminio.config;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
@RestController
public class TestController {
@Resource
private MinioUtil minioUtil;
@PostMapping("/upload")
public String upload(MultipartFile multipartFile) {
return minioUtil.upload(multipartFile);
}
}