MinIO上传
docker 搭建minio
yml
version: '3'
services:
minio:
image: minio/minio:RELEASE.2023-04-13T03-08-07Z
container_name: minio
ports:
# api 端口
- "9000:9000"
# 控制台端口
- "9001:9001"
environment:
# 时区上海
TZ: Asia/Shanghai
# 管理后台用户名
MINIO_ROOT_USER: root
# 管理后台密码,最小8个字符
MINIO_ROOT_PASSWORD: root123456
# https需要指定域名
#MINIO_SERVER_URL: "https://xxx.com:9000"
#MINIO_BROWSER_REDIRECT_URL: "https://xxx.com:9001"
# 开启压缩 on 开启 off 关闭
MINIO_COMPRESS: "off"
# 扩展名 .pdf,.doc 为空 所有类型均压缩
MINIO_COMPRESS_EXTENSIONS: ""
# mime 类型 application/pdf 为空 所有类型均压缩
MINIO_COMPRESS_MIME_TYPES: ""
volumes:
# 映射当前目录下的data目录至容器内/data目录
- /www/docker/minio/data:/data
# 映射配置目录
- /www/docker/minio/config:/root/.minio/
command: server --address ':9000' --console-address ':9001' /data # 指定容器中的目录 /data
privileged: true
network_mode: "host"
xml配置文件
xml
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.10</version>
</dependency>
yml配置文件
yml
minio:
endpoint: http://192.168.165.187:9000
accessKey: 5UldcOke8mfManPN
secretKey: xtMnHkrJR2SxAhACpubd5sya4uwV7LF5
bucket: wzfile
readPath: http://192.168.165.187:9000
获取yml配置参数
java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinIOConfigProperties implements Serializable {
private String accessKey;
private String secretKey;
private String bucket;
private String endpoint;
private String readPath;
}
minio 配置
java
import io.minio.MinioClient;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties({MinIOConfigProperties.class})
public class MinIOConfig {
private final MinIOConfigProperties minIOConfigProperties;
@Bean
public MinioClient buildMinioClient() {
return MinioClient
.builder()
.credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey())
.endpoint(minIOConfigProperties.getEndpoint())
.build();
}
}
上传服务类
java
import cn.hutool.core.util.ObjectUtil;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
@Service
@Slf4j
@RequiredArgsConstructor
public class FileServiceImpl implements IFileService {
private final MinioClient minioClient;
private final MinIOConfigProperties minIOConfigProperties;
@Override
public String uploadFileToCloud(MultipartFile file) {
if (ObjectUtil.isNull(file)){
log.error("上传文件为空");
throw new ApiException(ResultCode.ERROR,"上传文件为空");
}
try{
String fileName = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
// 获取当前日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String datePath = sdf.format(new Date());
// 组合存储对象名称
String objectName = datePath + "/" + generateFileNameWithRandom(fileName);
log.info("文件上传开始;{}",objectName);
minioClient.putObject(PutObjectArgs.builder()
.bucket(minIOConfigProperties.getBucket())
.object(objectName)
.stream(inputStream, inputStream.available(), -1)
.build());
FileBo fileBo = new FileBo();
String url = minIOConfigProperties.getEndpoint()+"/"+minIOConfigProperties.getBucket()+"/"+objectName;
return url ;
}catch (Exception e){
throw new ApiException(ResultCode.ERROR,e.getMessage());
}
}
/**
* 获取文件名
* @param fileName
* @return
*/
@Override
public String getFileName(String fileName) {
int lastIndex = fileName.lastIndexOf('.');
if (lastIndex!= -1) {
return fileName.substring(0, lastIndex);
}
return null;
}
/**
* 获取文件后缀
* @param fileName
* @return
*/
@Override
public String getFileSuffix(String fileName) {
if (fileName == null) {
return "";
}
int lastIndex = fileName.lastIndexOf('.');
if (lastIndex == -1) {
return "";
}
return fileName.substring(lastIndex);
}
private String generateFileNameWithRandom(String originalFileName) {
String baseName = originalFileName;
int lastIndex = originalFileName.lastIndexOf('.');
if (lastIndex!= -1) {
baseName = originalFileName.substring(0, lastIndex);
}
String randomPart = generateRandomPart();
String fileExtension = "";
if (lastIndex!= -1) {
fileExtension = originalFileName.substring(lastIndex);
}
return baseName + randomPart + fileExtension;
}
private String generateRandomPart() {
Random random = new Random();
int randomNumber = random.nextInt(9000) + 1000;
return "_" + randomNumber;
}
}