是什么
MinIO 是一个高性能的分布式对象存储服务,适合存储非结构化数据,如图片,音频,视频,日志等。对象文件最大可以达到5TB。
安装启动
shell
mkdir -p /usr/local/minio
cd /usr/local/minio
# 下载安装包
wget https://dl.min.io/server/minio/release/linux-amd64/minio
下载完毕后:
在当前minio目录下,会出现一个minio文件
shell
# 给予权限
chmod +x minio
# 创建minio文件存储目录及日志目录
mkdir -p /usr/local/minio/data
mkdir -p /usr/local/minio/logs
然后在 /usr/local/minio/目录下,新建一个run.sh并编辑以下内容
vim run.sh,然后将以下内容保存到run.sh,并为其赋予执行权限chmod u+x run.sh
run.sh内容如下:
sh
#!/bin/bash
export MINIO_ACCESS_KEY=minio
export MINIO_SECRET_KEY=minio123456
# nohup启动服务 指定文件存放路径 /root/data 还有设置日志文件路径 /root/minio/log
nohup ./minio server --address :9000 --console-address :9001 /usr/local/minio/data > /usr/local/minio/logs/minio.log 2>&1 &
启动minIO
shell
# 启动minio服务
./run.sh
# 查看日志
tail -f /usr/local/minio/logs/minio.log
平台使用
地址:http://127.0.0.1:9000(注意开放对应端口9000、9001)【当端口 9000
用于连接到 API 时,MinIO 会自动将浏览器访问重定向到 MinIO 控制台9001。】
输入账号密码后,
创建bucket,名字随意,创建完成后服务器/usr/local/minio/data下也会创建这个文件目录
http://www.dedeyun.com/it/java/102599.html
SpringBoot集成MinIO
1.添加依赖
xml
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.1</version>
</dependency>
2.配置文件
yaml
spring:
minio:
endpoint: http://124.222.253.33:9000
access-key: minio
secret-key: minio123456
bucket-name: file
servlet:
multipart: # 上传文件最大大小
max-file-size: 1024MB
max-request-size: 1024MB
3.配置类创建客户端
java
@Data
@Component
@ConfigurationProperties(prefix = "spring.minio")
public class MinioConfig {
private String endpoint;
private String accessKey;
private String secretKey;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
4.核心业务类
仅有一些基础操作
java
@Slf4j
@Service
public class FileService {
@Autowired
private MinioClient minioClient;
private static final String SEPARATOR = "/";
@Value("${spring.minio.bucket-name}")
private String bucketName;
/**
* 上传文件
*
* @param file 文件对象
* @param id 设备ID
*/
public void uploadFile(MultipartFile file, Long id) {
try {
// 创建bucket
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
// 同名文件会进行覆盖
String objectName = file.getOriginalFilename();
@Cleanup
InputStream inputStream = file.getInputStream();
minioClient.putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(id + SEPARATOR + objectName) // 多文件夹
.stream(inputStream, file.getSize(), -1)
.contentType(file.getContentType())
.build());
} catch (Exception e) {
log.error("文件上传失败!{}", e.getMessage());
throw new RuntimeException("文件上传失败,请重新上传");
}
}
/**
* 删除文件
*
* @param path 文件路径
*/
public void removeObject(String path) {
try {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(bucketName)
.object(path)
.build());
} catch (Exception e) {
log.error("文件删除失败!{}", e.getMessage());
throw new RuntimeException("文件删除失败,请重新删除");
}
}
/**
* 文件列表
*
* @param id 设备ID
*/
public List<FileInfo> listFiles(Long id) {
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).recursive(true).build());
List<FileInfo> infos = new ArrayList<>();
results.forEach(r -> {
FileInfo info = new FileInfo();
try {
Item item = r.get();
// 最多一层文件夹
String path = item.objectName();
int idx = path.indexOf(SEPARATOR);
if (path.substring(0, idx).equals(id.toString())) {
info.setPath(path);
info.setFileName(path.substring(idx + 1));
info.setSize(item.size());
infos.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
});
return infos;
}
/**
* 下载文件
*
* @param response response
* @param path 文件路径
*/
public void downFile(HttpServletResponse response, String path) {
try {
@Cleanup
OutputStream outputStream = response.getOutputStream();
@Cleanup
InputStream inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(this.bucketName)
.object(path).build());
response.reset();
String fileName = path.substring(path.indexOf(SEPARATOR) + 1);
response.setHeader("Content-Disposition", "attachment;filename=" +
URLEncoder.encode(fileName.substring(fileName.lastIndexOf(SEPARATOR) + 1), "UTF-8"));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
FileCopyUtils.copy(inputStream, outputStream);
} catch (Throwable e) {
log.error("文件下载失败!{}", e.getMessage());
throw new RuntimeException("文件下载失败,请重新下载");
}
}
}
controller
java
@RestController
public class FileController {
@Autowired
private FileService fileService;
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file, Long id) {
fileService.uploadFile(file, id);
return "上传成功";
}
@GetMapping("/download")
public String downloadFile(HttpServletResponse response, String path) {
fileService.downFile(response, path);
return "下载成功";
}
@DeleteMapping("/delete")
public String deleteFile(String path) {
fileService.removeObject(path);
return "删除成功";
}
@GetMapping("/list")
public List<FileInfo> listFiles(Long id) {
return fileService.listFiles(id);
}
}