linux常规安装启动
指定存储路径 /usr/software/minio/data
# 下载 miniowget https://dl.min.io/server/minio/release/linux-amd64/minio# 添加可执行权限chmod +x minio# 设置登录minio的 access keyexport MINIO_ACCESS_KEY=minioadmin# 设置登录minio的 secret keyexport MINIO_SECRET_KEY=minioadmin# 启动 minio./minio server /data
docker 容器启动
拉取镜像
docker pull minio/minio
运行容器,用-e指定容器环境变量
将MiniIO的数据和配置文件夹挂在到宿主机上 -v [宿主机路径]:[容器路径]server /mnt/data 命令指定容器里minio存储的路径
docker run -d -p 9000:9000 \
-p 9001:9001 \
--name minio \
-v /usr/local/minio/data:/mnt/data \
-v /usr/local/minio/config:/root/.minio \
-e "MINIO_ROOT_USER=minio" \
-e "MINIO_ROOT_PASSWORD=minio1234" \
minio/minio server /mnt/data --console-address ":9001"
整合Springboot
Maven依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.0</version>
</dependency>
配置读取类 MinIoProperties
@ConfigurationProperties(prefix = "minio")
@Data
public class MinIoProperties {
private String endpoint = "http://127.0.0.1:9000";
private String accessKey;
private String secretKey;
private String bucketName;
private int multipartSize = 5242880;
}
使用此类就可以直接读取application.yml的配置
minio:
endpoint: http://127.0.0.1:9000
accessKey: minio
secretKey: minio1234
bucketName: test
但是此类现在还不能被springboot扫描到 需要用到后面的配置类
接口封装类 MinIoComponent
import io.minio.*;
import io.minio.http.Method;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.io.InputStream;
@Slf4j
public class MinIoComponent {
private MinIoProperties properties;
private MinioClient client;
public MinIoComponent(MinIoProperties properties, MinioClient client) {
this.properties = properties;
this.client = client;
}
@SneakyThrows
public void putObject(InputStream inputStream, String objectName) {
this.checkBucket();
PutObjectArgs putObjectArgs = ((PutObjectArgs.builder().bucket(this.properties.getBucketName())).stream(inputStream, inputStream.available(), this.properties.getMultipartSize()).object(objectName)).build();
this.client.putObject(putObjectArgs);
}
@SneakyThrows
public InputStream getObject(String objectName) {
GetObjectArgs getObjectArgs = ((GetObjectArgs.builder().bucket(this.properties.getBucketName())).object(objectName)).build();
return this.client.getObject(getObjectArgs);
}
@SneakyThrows
public void removeObject(String objectName) {
RemoveObjectArgs removeObjectArgs = ((RemoveObjectArgs.builder().bucket(this.properties.getBucketName())).object(objectName)).build();
this.client.removeObject(removeObjectArgs);
}
@SneakyThrows
public String getObjectUrl(String objectName) {
GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = ((GetPresignedObjectUrlArgs.builder().bucket(this.properties.getBucketName())).object(objectName)).method(Method.GET).build();
return this.client.getPresignedObjectUrl(getPresignedObjectUrlArgs);
}
public boolean checkObjectExist(String objectName) {
StatObjectArgs statusObjectArgs = ((StatObjectArgs.builder().bucket(this.properties.getBucketName())).object(objectName)).build();
try {
this.client.statObject(statusObjectArgs);
return true;
} catch (Exception e) {
return false;
}
}
@SneakyThrows
private void checkBucket() {
String bucketName = this.properties.getBucketName();
if (!this.client.bucketExists((BucketExistsArgs.builder().bucket(bucketName)).build())) {
log.warn("指定的bucket: {} 不存在,将新建此bucket", bucketName);
this.client.makeBucket((MakeBucketArgs.builder().bucket(bucketName)).build());
}
}
}
Minio配置类
此类的作用是让yml的配置生成MinIoProperties单例对象
然后将接口封装的MinIoComponent类可以被依赖注入,直接引用
@Configuration
@EnableConfigurationProperties({MinIoProperties.class})
public class MinIoConfig {
public MinIoConfig() {
}
@Bean
@ConditionalOnMissingBean
public MinIoComponent minIoComponent(MinIoProperties properties) {
MinioClient client = MinioClient.builder().endpoint(properties.getEndpoint()).credentials(properties.getAccessKey(), properties.getSecretKey()).build();
return new MinIoComponent(properties, client);
}
}
