当前位置: 首页 > news >正文

Spring Boot集成MinIO的详细步骤

1. 安装MinIO

使用Docker部署MinIO
  1. 拉取MinIO镜像

docker pull minio/minio

这将从Docker Hub中获取最新的MinIO镜像。

创建目录

mkdir -p /home/minio/config
mkdir -p /home/minio/data

 这些目录将用于持久化MinIO的数据和配置文件

创建MinIO容器并运行: 

docker run -p 9000:9000 -p 9090:9090 \--net=host \--name minio \-d --restart=always \-e "MINIO_ACCESS_KEY=minioadmin" \-e "MINIO_SECRET_KEY=minioadmin" \-v /home/minio/data:/data \-v /home/minio/config:/root/.minio \minio/minio server /data --console-address ":9090" -address ":9000"
  1. 这将启动MinIO服务,使其可以通过主机的9000端口和9090端口进行访问。

  2. 登录MinIO控制台: 安装完成后,通过浏览器访问MinIO控制台,默认地址为 http://localhost:9000,使用设置的访问密钥和秘密密钥进行登录。

2. Spring Boot集成MinIO

添加依赖

pom.xml中添加以下依赖:

<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.5.2</version>
</dependency>
配置MinIO

application.properties中添加MinIO的配置:

minio.host=http://localhost:9000
minio.access-key=minioadmin
minio.secret-key=minioadmin
minio.bucket=test-bucket

创建MinIO配置类 

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.host}")private String host;@Value("${minio.access-key}")private String accessKey;@Value("${minio.secret-key}")private String secretKey;@Beanpublic MinioClient minioClient() {return MinioClient.builder().endpoint(host).credentials(accessKey, secretKey).build();}
}

创建存储桶 

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class MinioService {@Autowiredprivate MinioClient minioClient;public void createBucket(String bucketName) {if (!minioClient.bucketExists(b -> b.bucket(bucketName))) {minioClient.makeBucket(m -> m.bucket(bucketName));}}
}

文件上传 

import io.minio.MinioClient;
import io.minio.PutObjectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.io.InputStream;@Service
public class FileUploadService {@Autowiredprivate MinioClient minioClient;public String uploadFile(MultipartFile file, String bucketName, String objectName) throws Exception {try (InputStream inputStream = file.getInputStream()) {PutObjectResponse response = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, file.getSize(), -1).contentType(file.getContentType()).build());return "http://localhost:9000/" + bucketName + "/" + objectName;}}
}

文件下载 

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;@RestController
public class FileDownloadController {@Autowiredprivate MinioClient minioClient;@GetMapping("/download")public void downloadFile(@RequestParam String bucketName, @RequestParam String objectName, HttpServletResponse response) {try {InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());response.setContentType("application/octet-stream");response.setHeader("Content-Disposition", "attachment; filename=" + objectName);stream.transferTo(response.getOutputStream());} catch (Exception e) {e.printStackTrace();}}
}

通过以上步骤,你可以在Spring Boot中成功集成并使用MinIO进行文件存储和管理。 

 

 

 

 

 

 

 

 

 

相关文章:

  • 避坑,app 播放器media:MediaElement paly报错
  • 子函数嵌套的意义——以“颜色排序”为例(Python)
  • css 中float属性及clear的释疑
  • 小白如何从0学习CSS
  • Spark-SQL与Hive的连接及数据处理全解析
  • 关于STM32创建工程文件启动文件选择
  • 服务器带宽问题,以及服务器的上传下载速度,异常卡顿等
  • 通过建模和仿真进行高速连接器设计
  • 绿算轻舟系列FPGA加速卡:驱动数字化转型的核心动力【2】
  • SDK游戏盾ip可以破解吗
  • Elasticsearch 8.18 中提供了原生连接 (Native Joins)
  • 单例模式:懒汉和饿汉
  • 深入探索函数的奥秘:从基础到进阶的编程指南
  • uniapp(Vue)开发微信小程序 之 保存图片到本地
  • 其利天下即将亮相第21届(顺德)家电电源与智能控制技术研讨会
  • 确保连接器后壳高性能互连的完整性
  • Go-zero:JWT鉴权方式
  • 车载刷写架构 --- 刷写流程中重复擦除同一地址的问题分析
  • 【MySQL】索引事务
  • 把城市变成智能生命体,智慧城市的神奇进化
  • 商务网站主页设计公司/如何线上推广引流
  • 国外做多媒体展览的网站/推广普通话内容100字
  • 广州建设局官方网站/千万别手贱在百度上搜这些词
  • 公司网站建设公司/公司建官网要多少钱
  • 顺德做网站推广哪家好/快速提高排名
  • 平台网站很难做/优化营商环境的意义