pom.xml
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.0</version>
</dependency>
配置类
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
private String url;
private String accessKey;
private String secretKey;
private String bucket;
private String webUrl;
}
config类
@Configuration
@ConditionalOnProperty(name = "file.type", havingValue = "minio")
public class MinioConfig {
@Autowired
private MinioProperties minioProperties;
@Bean
public MinioClient minioClient() {
return MinioClient.builder().endpoint(minioProperties.getUrl())
.credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey()).build();
}
@Bean
public MinioUtil minioUtil(MinioProperties minioProperties, MinioClient minioClient) {
return new MinioUtil(minioProperties, minioClient);
}
}
yaml配置文件
file:
type: minio
minio:
url: http://xxx:9000
access-key: minioadmin
secret-key: minioadmin
bucket: test
web-url: http://xxx
工具类
public class MinioUtil {
public static final int DEFAULT_MULTIPART_SIZE = 10 * 1024 * 1024;
private MinioProperties minioProperties;
private MinioClient minioClient;
public MinioUtil(MinioProperties minioProperties, MinioClient minioClient) {
this.minioProperties = minioProperties;
this.minioClient = minioClient;
}
public MinioProperties getMinioProperties() {
return minioProperties;
}
public void createBucket(String bucket) throws Exception {
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
}
}
public List<String> getBuckets() throws Exception {
return minioClient.listBuckets().stream().map(Bucket::name).collect(Collectors.toList());
}
public void deleteBucket(String bucket) throws Exception {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
}
public String uploadFile(MultipartFile file) throws Exception {
String filePath = DateUtil.today() + "/" + file.getOriginalFilename();
minioClient.putObject(PutObjectArgs.builder()
.bucket(minioProperties.getBucket())
.object(filePath)
.stream(file.getInputStream(), -1, DEFAULT_MULTIPART_SIZE)
.build());
return filePath;
}
public InputStream downloadFile(String fileName) throws Exception {
InputStream stream = minioClient.getObject(GetObjectArgs.builder()
.bucket(minioProperties.getBucket())
.object(fileName).build());
return stream;
}
public void removeFile(String fileName) throws Exception {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(minioProperties.getBucket())
.object(fileName).build());
}
}
使用demo
private void uploadFile(MultipartFile file, FileInfo fileInfo) {
try {
String filePath = minioUtil.uploadFile(file);
fileInfo.setUrl(minioUtil.getMinioProperties().getWebUrl() + "/" + filePath);
fileInfo.setPath(filePath);
} catch (Exception e) {
log.error("uploadFile [{}]",e.getMessage());
}
}
public void downLoad(String fileName, String filePath, HttpServletResponse response) {
if (StrUtil.isNotBlank(fileName) && StrUtil.isNotBlank(filePath)) {
try {
InputStream inputStream = minioUtil.downloadFile(filePath);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setCharacterEncoding("UTF-8");
IoUtil.copy(inputStream,response.getOutputStream());
} catch (Exception e) {
log.error("downLoad [{}]",e.getMessage());
}
}
}
public void deleteFile(String id) {
try {
FileInfo fileInfo = getById(id);
if (fileInfo != null) {
removeById(fileInfo.getId());
minioUtil.removeFile(fileInfo.getPath());
}
} catch (Exception e) {
log.error("deleteFile [{}]",e.getMessage());
}
}