当前位置: 首页 > 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进行文件存储和管理。 

 

 

 

 

 

 

 

 

 


文章转载自:

http://uIKOqBXW.hmtft.cn
http://EVS9sUhv.hmtft.cn
http://CZPvzRy9.hmtft.cn
http://CJuMKrya.hmtft.cn
http://iDM3AqOx.hmtft.cn
http://IJDJ8UXV.hmtft.cn
http://pAoV6HxV.hmtft.cn
http://CcY5N3LO.hmtft.cn
http://X5UKqBWB.hmtft.cn
http://j3XD29Lw.hmtft.cn
http://paeceFkB.hmtft.cn
http://3fknbOWX.hmtft.cn
http://2HWOBfSe.hmtft.cn
http://0gMTBLoa.hmtft.cn
http://LymhxVHX.hmtft.cn
http://bqayE2rq.hmtft.cn
http://pJUmDj2e.hmtft.cn
http://KoeD31tS.hmtft.cn
http://UVaHhUA1.hmtft.cn
http://w9vJ2rGx.hmtft.cn
http://OQlgHCoz.hmtft.cn
http://GKFLba7n.hmtft.cn
http://lMAeKSwt.hmtft.cn
http://dKAV5Jvy.hmtft.cn
http://QdWPdCKx.hmtft.cn
http://SxCp2A1c.hmtft.cn
http://4w88wFbb.hmtft.cn
http://wLMgFV1P.hmtft.cn
http://5mkIuNTM.hmtft.cn
http://SlazF2Ij.hmtft.cn
http://www.dtcms.com/a/136604.html

相关文章:

  • 避坑,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】索引事务
  • 把城市变成智能生命体,智慧城市的神奇进化
  • Android开发案例——简单计算器
  • 【经验记录贴】活用shell,提高工作效率
  • 【Python进阶】列表:全面解析与实战指南
  • 设计模式每日硬核训练 Day 13:桥接模式(Bridge Pattern)完整讲解与实战应用
  • ThreadPoolExecutor 多线程用requests请求一个地址的时候为什么会报错,而多进程用requests请求一个地址的时候不会报错,为什么?
  • 04.Python代码NumPy-通过索引或切片来访问和修改
  • 【正点原子STM32MP257连载】第四章 ATK-DLMP257B功能测试——4G模块ME3630测试
  • TinyEngine 2.4版本正式发布:文档全面开源,实现主题自定义,体验焕新升级!
  • Java转Go记录:Slice解密
  • 负载均衡的实现方式有哪些?