1.pom minio依赖

2.application.yml中添加minio参数

minio上传文件_ide


3.config下

读取yml中的minio配置信息

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "xxx.minio")
public class Minio {

private String MINIO_URL;

private String MINIO_NAME;

private String MINIO_PASS;

private String BUCKET_NAME;
}
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinioConfiguration {

@Autowired
Minio minio;

@Bean
public MinioClient getMinioClient() throws InvalidPortException, InvalidEndpointException {
return new MinioClient(minio.getMINIO_URL(), minio.getMINIO_NAME(), minio.getMINIO_PASS());
}
}

4.service下

import org.springframework.web.multipart.MultipartFile;

public interface IMinioService {

String upload(MultipartFile multipartFile) throws Exception;

int delete(String url);
}

sdf.format(new Date()) + “/” + filename; 这个/可以起到minio文件夹的划分作用

import cn.vpclub.preresearch.modules.sdkmanager.config.Minio;
import cn.vpclub.preresearch.modules.sdkmanager.service.IMinioService;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class MinioServiceImpl implements IMinioService {

@Autowired
Minio minio;

@Autowired
MinioClient minioClient;

@Override
public String upload(MultipartFile multipartFile) throws Exception{
String bucketName = minio.getBUCKET_NAME();
boolean bucketExists = minioClient.bucketExists(bucketName);
if(bucketExists){
log.info("Bucket already exists....");
} else {
minioClient.makeBucket(bucketName);
}
String filename = multipartFile.getOriginalFilename();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String objectName = sdf.format(new Date()) + "/" + filename;
PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), 5242880L);
putObjectOptions.setContentType(multipartFile.getContentType());
minioClient.putObject(bucketName,objectName,multipartFile.getInputStream(),putObjectOptions);
log.info("文件上传成功!");
return minio.getMINIO_URL()+"/"+bucketName+"/"+objectName;
}

@Override
public int delete(String url) {
return 0;
}
}

5.controller方法

/**
* 文件上传统一方法
* @param request
* @param response
* @return
*/
@PostMapping(value = "/upload")
public XXResult<?> upload(HttpServletRequest request, HttpServletResponse response) {
XXResult<?> result = new VpResult<>();
String savePath = "";
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获取上传文件对象
MultipartFile file = multipartRequest.getFile("file");
try {
savePath = minioService.upload(file);
} catch (Exception e) {
log.error("上传失败!");
e.printStackTrace();
}
if(oConvertUtils.isNotEmpty(savePath)){

int i = savePath.indexOf("/"+this.bucketName+"/") + + (this.bucketName.length() + 2);
String returnPath = savePath.substring(i);
String url = downloadPath + request.getContextPath();
result.setMessage(url + "/admin/sys/common/download?path=" + returnPath);
result.setSuccess(true);
}else {
result.setMessage("上传失败!");
result.setSuccess(false);
}
return result;
}

minio上传文件_ios_02

minio上传文件_spring_03


参考:https://gitee.com/jarcheng/movieSystem-Springcloud/tree/master/cheng-upload/