1.Minio搭建
官网下载 https://docs.min.io/cn/
Windows版不用安装,直接命令运行:minio.exe server D:\Photos
D:\Photos表示文件存放位置
看到这里就是运行成功,,最低的红色提示改密码,可先忽略。
默认账户密码
AccessKey: minioadmin
SecretKey: minioadmin
创建一个bucket,并设置只读权限,不然不能在浏览器直接打开文件链接进行查看。
2.Springboot集成
1.创建一个springboot项目
2.pom加上minio依赖
> >io.minio</groupId> >minio</artifactId> >8.0.3</version></dependency>
完整文件
xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.4.1version> <relativePath/> parent> <groupId>com.hmgroupId> <artifactId>minioartifactId> <version>0.0.1-SNAPSHOTversion> <name>minioname> <description>Demo project for Spring Bootdescription> <properties> <java.version>11java.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>io.miniogroupId> <artifactId>minioartifactId> <version>8.0.3version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> dependency> dependencies> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build>project>
application.yml
server: port: 7009spring: application: name: miniominio: #minio服务器地址 endpoint: http://127.0.0.1:9000 accessKey: minioadmin secretKey: minioadmin #对外域名,非必需 minioHost: ${minio.endpoint}
Minio相关配置文件:
1.读取application.yml的minio配置
新建MinioProp.java文件,不要命名为MinioProperties,会冲突
package com.hm.minio.config;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Data@Component@ConfigurationProperties(prefix = "minio")public class MinioProp { private String endpoint; private String accessKey; private String secretKey; private String minioHost;}
2.配置MinioClient,设置服务器地址、用户名、密码
新建MinioConfig.java
package com.hm.minio.config;import io.minio.MinioClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration@EnableConfigurationProperties(MinioProp.class)public class MinioConfig { @Autowired private MinioProp minioProp; @Bean public MinioClient minioClient() { return MinioClient.builder() .endpoint(minioProp.getEndpoint()) .credentials(minioProp.getAccessKey(), minioProp.getSecretKey()) .build(); }}
创建Minio工具类,实现文件上传等功能
新建MinioUtil.java
package com.hm.minio.utils;import com.hm.minio.config.MinioProp;import io.minio.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import org.springframework.web.multipart.MultipartFile;@Componentpublic class MinioUtil { public static final String DEFAULT_BUCKET_NAME="default"; @Autowired private MinioProp minioProp; @Autowired private MinioClient minioClient; public String putObject(MultipartFile file, String bucketName) throws Exception { //文件不能为空 if (null == file || 0 == file.getSize()) { return null; } if (!StringUtils.hasText(bucketName)) { bucketName = DEFAULT_BUCKET_NAME; } //判断存储桶是否存在,不存在则创建 if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); } //获取原始文件名 String fileName = file.getOriginalFilename(); //重命名文件 String objectName = System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf(".")); //上传文件 minioClient.putObject( PutObjectArgs.builder().bucket(bucketName).object(objectName).stream( file.getInputStream(), file.getSize(), -1) .contentType(file.getContentType()) .build()); //返回文件链接 return minioProp.getMinioHost() + "/" + bucketName + "/" + objectName;; }}
Controller
package com.hm.minio.controller;import com.hm.minio.utils.MinioUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;@RestController@RequestMapping("/minio")public class FileController { @Autowired private MinioUtil minioUtil; @PostMapping("/upload") public String upload(@RequestParam(name = "file") MultipartFile file) { String fileUrl = null; try { fileUrl = minioUtil.putObject(file, null); } catch (Exception e) { e.printStackTrace(); } return fileUrl; }}
查询bucket 、删除文件等其他操作请参考
https://github.com/minio/minio-java/tree/release/examples
https://docs.minio.io/cn/java-client-quickstart-guide.html