Docker 安装Minio
docker pull minio/minio
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
minio/minio latest 162489e21d26 7 days ago 165MB
nginx latest 7f553e8bbc89 7 days ago 192MB
# 外挂磁盘存储使用
mkdir -p /usr/local/minio/data
# 外挂配置文件使用
mkdir -p /usr/local/minio/config
# 创建minio 容器
docker run -d --name minio -p 9000:9000 -p 9001:9001 -v /usr/local/minio/data:/data -v /usr/local/minio/config:/root/.minio -e MINIO_ROOT_USER=admin -e MINIO_ROOT_PASSWO
RD=admin minio/minio server /data --console-address ":9001"
# 创建失败
docker logs minio
FATAL Unable to validate credentials inherited from the shell environment: Invalid credentials
> Please provide correct credentials
HINT:
MINIO_ROOT_USER length should be at least 3, and MINIO_ROOT_PASSWORD length at least 8 characters
# 密码不能少于8个字符
docker rm minio
docker run -d --name minio -p 9000:9000 -p 9001:9001 -v /usr/local/minio/data:/data -v /usr/local/minio/config:/root/.minio -e MINIO_ROOT_USER=admin -e MINIO_ROOT_PASSWO
RD=admin123 minio/minio server /data --console-address ":9001"
# 防火墙开放端口
firewall-cmd --zone=public --add-port=9000/tcp --permanent
firewall-cmd --zone=public --add-port=9001/tcp --permanent
# 重启防火墙
sudo firewall-cmd --reload
访问测试
http://ip:9001
SpringBoot 上传文件到Minio
pom文件引入依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.2</version>
</dependency>
application.properties 配置Minio
spring.minio.access-key=pNehtjUDw9ENBlULVTGT
spring.minio.secret-key=p3yVngZ2Ei1uCtn6vKozrof6ICB7GFBiYPr5RAWW
spring.minio.endpoint=http://192.168.10.227:9000
配置MinioClinet
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.minio")
@Slf4j
public class MinioConfig {
private String accessKey;
private String secretKey;
private String endpoint;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
FileService
public interface FileService {
/**
* 是否存在桶
*/
Boolean bucketExists(String bucketName);
/**
* 创建桶
*/
void makeBucket(String bucketName);
/**
* 删除桶
*/
void removeBucket(String bucketName);
/**
* 获取当所有桶
*/
List<Bucket> getAllBuckets();
/**
* 上传文件
*/
String uploadFile(String bucketName, String prefix, MultipartFile file) throws Exception;
/**
* 文件预览
*/
String preview(String bucketName, String prefix, String fileName) throws Exception;
/**
* 文件下载
*/
void download(String bucketName, String prefix, String fileName, HttpServletResponse res);
/**
* 删除文件
*/
Boolean removeFile(String bucketName, String prefix, String fileName);
/**
* 文件是否存在
*/
boolean isFileExists(String bucketName, String prefix, String fileName) throws Exception;
/**
* 生成一个带到期时间、签名的URL,这个地址可以提供给没有登录的第三方共享访问或者上传对象,针对于Bucket为私有的情况
*/
String getPresignedObjectUrl(String bucketName, String prefix, String fileName);
}
FileServiceImpl
package com.example.demo.service.impl;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.io.file.FileNameUtil;
import com.example.demo.service.FileService;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
/**
* @author :王建
* @since :2024-10-10 11:38
*/
@Service
@Slf4j
public class FileServiceImpl implements FileService {
@Autowired
private MinioClient minioClient;
@Override
public Boolean bucketExists(String bucketName) {
try {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void makeBucket(String bucketName) {
try {
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void removeBucket(String bucketName) {
try {
minioClient.removeBucket(RemoveBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<Bucket> getAllBuckets() {
try {
return minioClient.listBuckets();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public String uploadFile(String bucketName, String prefix, MultipartFile file) throws Exception {
String uuid = UUID.randomUUID().toString();
// 重新命名原来文件
String fileName = uuid + "." + FileNameUtil.extName(file.getOriginalFilename());
log.info("源文件名称:{},重命名后文件名称:{}", file.getOriginalFilename(), fileName);
String objectName = StringUtils.isBlank(prefix) ? fileName : "/" + prefix + "/" + fileName;
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName).object(objectName)
.stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
minioClient.putObject(objectArgs);
return uuid;
}
@Override
public String preview(String bucketName, String prefix, String fileName) throws Exception {
// 查看文件地址
String objectName = StringUtils.isBlank(prefix) ? fileName : "/" + prefix + "/" + fileName;
GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName).method(Method.GET).build();
return minioClient.getPresignedObjectUrl(build);
}
@Override
public void download(String bucketName, String prefix, String fileName, HttpServletResponse res) {
}
@Override
public Boolean removeFile(String bucketName, String prefix, String fileName) {
return null;
}
@Override
public boolean isFileExists(String bucketName, String prefix, String fileName) throws Exception {
String objectName = StringUtils.isBlank(prefix) ? fileName : "/" + prefix + "/" + fileName;
StatObjectResponse statObjectResponse = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
return !Objects.isNull(statObjectResponse);
}
@Override
public String getPresignedObjectUrl(String bucketName, String prefix, String fileName) {
return null;
}
}