这里我选用的是JDK1.8 + SpringBoot2.3.12.RELEASE
一、导入依赖
java"><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.2.2</version>
</dependency>
二、导入工具类
注意:需要在 endpoint、bucketName、accessKey、secretKey 中指定配置信息
java">import io.minio.*;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;@Component
public class MinioUtil {// minio服务器ipprivate String endpoint="http://192.168.31.208:5000";// 桶名private String bucketName="minio-test";// access keyprivate String accessKey="minio-test";// secret keyprivate String secretKey="minio-test";/*** @param fileInputStream 要上传文件的输入流* @param minioFileName 推送到minio之后的名称* @return*/public String upload(InputStream fileInputStream, String minioFileName){// 1. 拿到minio客户端MinioClient minioClient= MinioClient.builder().endpoint(endpoint).credentials(accessKey,secretKey).build();// 2. 判断bucket是否存在,若不存在,则创建buckettry {if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())){minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());}}catch (Exception e) {e.printStackTrace();};// 3. 上传文件try {minioClient.putObject(PutObjectArgs.builder().object(minioFileName).bucket(bucketName).stream(fileInputStream, fileInputStream.available(), -1).build());}catch (Exception e){e.printStackTrace();System.out.println("上传失败");return null;}String url=endpoint+"/"+bucketName+"/"+minioFileName;return url;}/*** @param multipartFile 要上传的文件* @param minioFileName 要上传到minio的文件名* @return*/public String upload(MultipartFile multipartFile, String minioFileName){String url=null;try {url = upload(multipartFile.getInputStream(),minioFileName);} catch (IOException e) {e.printStackTrace();}return url;}/*** @param multipartFile 要上传的文件*/public String upload(MultipartFile multipartFile){String url=null;try {String minioFileName = UUID.randomUUID() + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));url = upload(multipartFile.getInputStream(),minioFileName);} catch (IOException e) {e.printStackTrace();}return url;}/*** 下载文件* @param fromMinioFileName 要下载的minio中的文件名* @param toLocalFileName 下载到本地的位置*/public void download(String fromMinioFileName,String toLocalFileName){// 1.创建minio客户端MinioClient minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey,secretKey).build();// 2.判断桶是否存在try {if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())){System.out.println(bucketName+" 这个桶不存在!");return;}} catch (Exception e) {e.printStackTrace();}// 3.下载文件try {minioClient.downloadObject(DownloadObjectArgs.builder().bucket(bucketName).object(fromMinioFileName).filename(toLocalFileName).build());} catch (Exception e) {e.printStackTrace();System.out.println(fromMinioFileName+" 下载失败!");}}
}
三、在application.yml文件里添加配置
上传视频时,有的视频内存过大,需要在配置文件中配置最大文件内存上限
spring:servlet:multipart:max-file-size: 200MB # 文件的最大尺寸100MBmax-request-size: 200MB # 单次请求可以接收文件的最大尺寸为100MB,一般上传视频时会用到
四、上传文件的控制层Controller
java">import com.xxx.springminio.util.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;@RestController
public class MinioTestController {@Autowiredprivate MinioUtil minioUtil;@PostMapping("/upload")public String upload(MultipartFile file) throws IOException {return minioUtil.upload(file);}
}