1.安装与配置Minio
https://dl.min.io/server/minio/release/windows-amd64/https://dl.min.io/server/minio/release/windows-amd64/ cmd的方式打开minio.exe所在的文件夹
1.1 设置管理员账号和密码
#这俩命令要与下面的运行命令一起同一命令框运行
set MINIO_ROOT_USER=minioadmin
set MINIO_ROOT_PASSWORD=minioadmin
1.2 执行以下指令启动minio
set MINIO_ROOT_USER=minioadmin
set MINIO_ROOT_PASSWORD=minioadminminio.exe server P:\MinIo\minio --console-address ":9001" --address ":9000"
linux启动minio命令
nohup /usr/local/bin/minio server --address ":9001" --console-address ":9000" /root/minio/data > minio.log 2>&1 &
1.3 浏览器上访问9001端口
localhost:9001
登陆后可以创建buket
2.微服务项目集成Minio
2.1 导入依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.2</version>
</dependency>
2.2 创建配置文件
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MinIOProperties.class)
public class MinIOConfig {
@Bean
public MinioClient minioClient(MinIOProperties minIOProperties) throws InvalidPortException, InvalidEndpointException {
// 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
MinioClient minioClient = MinioClient.builder()
.endpoint(minIOProperties.getEndpoint())
.credentials(minIOProperties.getAccessKey(),minIOProperties.getSecretKey())
.build();
return minioClient;
}
}
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "minio")
public class MinIOProperties {
private String endpoint;
private String accessKey;
private String secretKey;
private String bucketName;
}
2.3 创建工具类
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
@Service
public class MinIOService {
@Autowired
private MinioClient minioClient;
@Autowired
private MinIOProperties properties;
/**
* 上传文件
* @param file
* @return
*/
public String upload(MultipartFile file) {
// 原文件名
String originalFilename = file.getOriginalFilename();
// 获取文件的后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
// 构造新的文件名,名字不重复
String objectName = UUID.randomUUID().toString() + suffix;
// 上传文件
try {
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.contentType(file.getContentType())
.stream(file.getInputStream(), file.getSize(), -1)
.bucket(properties.getBucketName())
.object(objectName)
.build();
minioClient.putObject(putObjectArgs);
} catch (Exception e) {
throw new RuntimeException("上传文件失败: " + e.getMessage());
}
return properties.getEndpoint() + "/" + properties.getBucketName() + "/" + objectName;
}
/**
* 上传文件
* @param name
* @param inputStream
* @param contentType
* @return
*/
public String upload(String name,InputStream inputStream,String contentType){
// 上传文件
try {
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.contentType(contentType)
.stream(inputStream, inputStream.available(), -1)
.bucket(properties.getBucketName())
.object(name)
.build();
minioClient.putObject(putObjectArgs);
} catch (Exception e) {
throw new RuntimeException("上传文件失败: " + e.getMessage());
}
return properties.getEndpoint() + "/" + properties.getBucketName() + "/" + name;
}
/**
* 删除文件
* @param url
*/
public void delete(String url){
String objectName = url.replace(properties.getEndpoint()+"/","")
.replace(properties.getBucketName()+"/","");
RemoveObjectArgs args = RemoveObjectArgs.builder()
.bucket(properties.getBucketName())
.object(objectName)
.build();
try {
minioClient.removeObject(args);
} catch (Exception e) {
throw new RuntimeException("删除文件失败: " + e.getMessage());
}
}
/**
* 下载文件
* @param url
* @return
*/
public InputStream download(String url){
String objectName = url.replace(properties.getEndpoint()+"/","")
.replace(properties.getBucketName()+"/","");
GetObjectArgs args = GetObjectArgs.builder()
.bucket(properties.getBucketName())
.object(objectName)
.build();
InputStream inputStream = null;
try {
inputStream = minioClient.getObject(args);
} catch (Exception e) {
throw new RuntimeException("下载文件失败: " + e.getMessage());
}
return inputStream;
}
}
2.4 单服务项目中添加配置
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan({"com.luobei.common.minio"})
public class MinIoConfig2 { //这个名称不要和 common中的重复
}
2.5 单服务配置文件添加minio配置
minio:
endpoint: http://xxx.xxx.xxx.xxx:9000
accessKey: minioadmin
secretKey: minioadmin
bucketName: minioadmin
2.6 修改单服务配置文件文件大小限制(默认1MB)
spring:
servlet:
multipart:
max-file-size: 5MB