MiniO在Springboot中的配置

595 阅读2分钟

Minio是什么?

MinIO 是一款高性能、分布式、云原生的对象存储系统,专为大规模数据存储场景设计。它兼容 Amazon S3 API,常用于存储非结构化数据(如图片、视频、日志、备份等),并广泛应用于云原生、大数据、AI/ML 等领域。

Minio的优点?

1. 极简与轻量化

  • 安装部署仅需单行命令,无需复杂依赖。
  • 资源占用极低(仅需数百MB内存),适合容器化(如 Kubernetes)和边缘计算场景。
  • 提供直观的 Web 控制台和命令行工具,管理便捷。

2. 高性能

  • 基于 Go 语言开发,采用高效的算法(如纠删码),单节点即可实现高速读写。
  • 在基准测试中,MinIO 的吞吐量可达到 183 GB/s 读取和 171 GB/s 写入(官方数据),适合高并发场景。

在Springboot中配置Minio?

在安装目录下cmd开启服务。

image.png

执行开启命令

minio.exe server D:\minio\data --address "127.0.0.1:9000" --console-address "127.0.0.1:9001"

在Minio创建桶

image.png

添加maven依赖

<dependency> 
<groupId>io.minio</groupId> 
<artifactId>minio</artifactId> 
<version>8.5.9</version> 
</dependency>

在配置类中连接本地minio

image.png

配置一个 MinioClient 实例,供 Spring 应用程序使用,通常用于连接和操作 MinIO 存储服务。

package com.xyu.Config;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MinioConfig {
    @Value("${minio.endpoint}")
    private String endpoint;
    
    @Value("${minio.accessKey}")
    private String accessKey;
    
    @Value("${minio.secretKey}")
    private String secretKey;

    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }
}

上传文件

代码
// 上传文件
@SneakyThrows
public String uploadFile(MultipartFile file) {
    ensureBucketExists();
    String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
    minioClient.putObject(
            PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .contentType(file.getContentType())
                    .build());
    return fileName;
}
@Autowired
private MinioService minioService;
// 上传接口
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) {
    return minioService.uploadFile(file);
}
效果

image.png

image.png

下载文件

// 下载接口
@GetMapping("/download/{fileName}")
public ResponseEntity<InputStreamResource> download(@PathVariable String fileName) throws Exception {
    GetObjectResponse response = (GetObjectResponse) minioService.downloadFile(fileName);
    return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="" + fileName + """)
            .contentType(MediaType.parseMediaType(response.headers().get("Content-Type")))
            .contentLength(response.headers().size())
            .body(new InputStreamResource(response));
}
@SneakyThrows
public GetObjectResponse downloadFile(String fileName) throws Exception {
    return minioClient.getObject(
            GetObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .build());
}

image.png

删除文件

// 删除接口
@DeleteMapping("/delete/{fileName}")
public String delete(@PathVariable String fileName) {
    minioService.deleteFile(fileName);
    return "File deleted: " + fileName;
}
// 删除文件
@SneakyThrows
public void deleteFile(String fileName) {
    minioClient.removeObject(
            RemoveObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .build());
}

image.png

image.png