文件服务器在用minio,没有独立成微服务也没有抽取starter,哪里要用往哪里拷,所以简单测试一下集成和抽取starter
1. 先不管抽取starter的事儿,创建springboot项目集成minio把文件上传成功
抽取前的demo结构:
pom:
<?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.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jxwy</groupId>
<artifactId>minio-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>minio-spring-boot-starter</name>
<description>minio-spring-boot-starter</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
minio.url=https://xxxxxx.xxx
minio.url_proxy=https://xxxxxx/
minio.name=xxxxxxx
minio.password=xxxxxx
MinioProperties:
@ConfigurationProperties(prefix = "minio")
@Component
public class MinioProperties {
private String url;
private String urlProxy;
private String name;
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrlProxy() {
return urlProxy;
}
public void setUrlProxy(String urlProxy) {
this.urlProxy = urlProxy;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MinioAutoConfiguration:
@Configuration
public class MinioAutoConfiguration {
//创建MinioClient的主要方法
@Bean
public MinioClient minioClient(MinioProperties properties){
MinioClient.Builder builder = MinioClient.builder();
builder.endpoint(properties.getUrl());
if (StringUtils.hasLength(properties.getName()) && StringUtils.hasLength(properties.getPassword())) {
builder.credentials(properties.getName(),properties.getPassword());
}
return builder.build();
}
}
MinioService:
public interface MinioService {
//桶是否存在
Boolean bucketExists(String bucket);
//创建桶
void makeBucket(String bucket);
//上传文件
void putObject(InputStream stream, String bucket, String objectname);
}
MinioServiceImpl:
@Service
public class MinioServiceImpl implements MinioService {
@Autowired
MinioClient minioClient;
@Override
public Boolean bucketExists(String bucket) {
try {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public void makeBucket(String bucket) {
try {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void putObject(InputStream stream,String bucket,String name) {
try {
ObjectWriteResponse objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(name).stream(stream, stream.available(), -1).build());
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试类中上传文件成功:
@SpringBootTest(classes =MinioSpringBootStarterApplication.class)
class MinioSpringBootStarterApplicationTests {
@Autowired
MinioService minioService;
@Test
void putObject() {
try {
FileInputStream in = new FileInputStream(new File("C:\\pictures\\1.jpg"));
//还没有封装方法
//文件的输入流,这里需要手动先创建件桶,指定文件名
minioService.putObject(in,"study-code","test.jpg");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
2.提取了starter
去掉pom中的build、test组件,有的文章说需要引入spring-boot-starter,测试了下不引入也可以。剩下的依赖:
<dependencies>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
去掉主入口函数,去掉application.properties配置文件(新建一个测试用的springboot项目,把配置文件拿过去)
剩下最重要的一步:在resources下创建META-INF/spring.factories文件,配置文件中加入需要自动装配的类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jxwy.miniostarter.service.impl.MinioServiceImpl,\
com.jxwy.miniostarter.config.MinioAutoConfiguration
打包安装到maven仓库,本地测试用的同一仓库地址的话可以直接maven install
测试:新建一个springboot项目,填入application.properties,pom中增加starter的依赖
<dependency>
<groupId>com.jxwy</groupId>
<artifactId>minio-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
再写个测试或者直接拷贝之前的测试。上传成功。