Java实现文件上传至Minio
首先导入所需依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>3.0.12</version>
</dependency>
配置文件中加入相关配置
# minio配置
minio:
url: 路径
accessKey: 账号
secretKey: 密码
useSSL: 是否使用https
Controller中代码
@RestController
@RequestMapping("/file")
@Slf4j
public class FileUpLoadController {
@Value("${minio.url}")
private String url;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@PostMapping("/uploadFile")
public AjaxResult uploadFile(@RequestParam("imgFile") MultipartFile file) throws Exception {
MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
InputStream is = file.getInputStream();
String fileName = file.getOriginalFilename().replace("-", "");
String contentType = file.getContentType();
boolean bucketExists = minioClient.bucketExists("test-bucket");
if (!bucketExists) {
minioClient.makeBucket("test-bucket");
}
minioClient.putObject("test-bucket", fileName, is, contentType);
String url = minioClient.presignedGetObject("test-bucket", fileName);
log.info(fileName + "文件上传成功");
return AjaxResult.success(url + "文件上传成功");
}
前端代码
<el-form-item label-width="100px" label="上传图片" prop="uploadImg">
<el-upload ref="pic" :headers="headers" accept="image/*" :on-success="uploadSuccess" :file-list="imgList"
:action="imgAction" :before-upload="beforeUpload" name="imgFile">
<el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
</el-upload>
</el-form-item>