news 2026/6/11 9:22:45

minio安装部署及使用

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
minio安装部署及使用

一、服务器安装minio

1.进行下载

下载地址:

GNU/Linux

https://dl.min.io/server/minio/release/linux-amd64/minio

2.新建minio安装目录,执行如下命令

mkdir -p /home/minio/data

把二进制文件上传到安装目录后,执行:

chmod +x minio //给予权限 export MINIO_ACCESS_KEY=minioadmin //创建账号 export MINIO_SECRET_KEY=minioadmin //创建密码 ./minio server /home/minio/data //启动

后台启动,并打印日志

nohup ./minio server /home/minio/data > /home/minio/data/minio.log &

默认的端口为:9000

自定义端口方式:自定义启动端口号以及控制台端口号,不设置则控制台会自动配置其他端口号,非常不方便

nohup ./minio server --address :9000 --console-address :9001 /home/minio/data > /home/minio/data/minio.log &

查看状态

ps -ef|grep minio

二、进行访问,并设置桶

1.访问

地址:http://127.0.0.1:9000

输入账号密码后:

进行创建桶,名字自取,创建完成后服务器home/minio/data下也会创建这个文件目录

进行设置:

必须将规则设置成readwrite,才可进行读取文件,否则只存或者只能读。

三、springboot进行实现

1.引入依赖

<!-- minio 相关依赖 --> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>3.0.10</version> </dependency> <!-- alibaba的fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.51</version> </dependency> <!-- thymeleaf模板引擎 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

2.在 application.yml 文件中加入 MinIO 服务器的相关信息

# minio 文件存储配置信息 minio: endpoint: http://127.0.0.1:9000 accesskey: minioadmin secretKey: minioadmin

3.创建实体类

这一步,我们将配置文件中 minio 的配置信息通过注解的方式注入到 MinioProp 这个实体中,方便后面我们使用

import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * minio 属性值 */ @Data @Component @ConfigurationProperties(prefix = "minio") public class MinioProp { /** * 连接url */ private String endpoint; /** * 用户名 */ private String accesskey; /** * 密码 */ private String secretKey; }

4、创建核心配置类

通过注入 MinIO 服务器的相关配置信息,得到 MinioClient 对象,我们上传文件依赖此对象

import io.minio.MinioClient; import io.minio.errors.InvalidEndpointException; import io.minio.errors.InvalidPortException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * minio 核心配置类 */ @Configuration @EnableConfigurationProperties(MinioProp.class) public class MinioConfig { @Autowired private MinioProp minioProp; /** * 获取 MinioClient * * @return * @throws InvalidPortException * @throws InvalidEndpointException */ @Bean public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException { return new MinioClient(minioProp.getEndpoint(), minioProp.getAccesskey(), minioProp.getSecretKey()); } }

5、上传工具类

import com.alibaba.fastjson.JSONObject; import com.zyxx.email.common.redis.RedisUtil; import com.zyxx.email.utils.DateUtils; import io.minio.MinioClient; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; @Slf4j @Component public class MinioUtils { @Autowired private MinioClient client; @Autowired private MinioProp minioProp; /** * 创建bucket * * @param bucketName bucket名称 */ @SneakyThrows public void createBucket(String bucketName) { if (!client.bucketExists(bucketName)) { client.makeBucket(bucketName); } } /** * 上传文件 * * @param file 文件 * @param bucketName 存储桶 * @return */ public JSONObject uploadFile(MultipartFile file, String bucketName) throws Exception { JSONObject res = new JSONObject(); res.put("code", 0); // 判断上传文件是否为空 if (null == file || 0 == file.getSize()) { res.put("msg", "上传文件不能为空"); return res; } try { // 判断存储桶是否存在 createBucket(bucketName); // 文件名 String originalFilename = file.getOriginalFilename(); // 新的文件名 = 存储桶名称_时间戳.后缀名 String fileName = bucketName + "_" + System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf(".")); // 开始上传 client.putObject(bucketName, fileName, file.getInputStream(), file.getContentType()); res.put("code", 1); res.put("msg", minioProp.getEndpoint() + "/" + bucketName + "/" + fileName); return res; } catch (Exception e) { log.error("上传文件失败:{}", e.getMessage()); } res.put("msg", "上传失败"); return res; } }

6.controller接口

import com.alibaba.fastjson.JSONObject; import com.zyxx.email.common.minio.MinioUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; @Controller public class MinioController { @Autowired private MinioUtils minioUtils; /** * 上传 * * @param file * @param request * @return */ @PostMapping("/upload") @ResponseBody public String upload(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) { JSONObject res = null; try { res = minioUtils.uploadFile(file, "product"); } catch (Exception e) { e.printStackTrace(); res.put("code", 0); res.put("msg", "上传失败"); } return res.toJSONString(); } }

测试

通过网址进行访问:

PostMan进行测试上传;

删除文件:

//文件删除 @DeleteMapping public String delete(String name) { try { MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY); minioClient.removeObject(BUCKETNAME, name); } catch (Exception e) { return "删除失败"+e.getMessage(); } return "删除成功"; }

适用于minio的文件内容文件路径加解密工具类(AES对称加密)

在使用minio的情况下,官方提供两种方案来做对象加密,分别是SSE-C、SSE-S3。但是在某些情况下我们受限于条件没法快速的通过以上方案实现加密,那么这个工具类可以帮助你,此加密过程经测试效率很不错,5MB的文件加密解密整个过程在800ms以内。

import cn.hutool.crypto.symmetric.SymmetricAlgorithm; import cn.hutool.crypto.symmetric.SymmetricCrypto; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Base64; import io.minio.MinioClient; public class EncryptionUtil { static final String originKeyStr = "0123456789Abc@@@"; // 必须16个字符 private static SymmetricCrypto aes; // 加密并编码字符串 public static String encryptURLEncodeStr(String str) { try { if (aes == null) { SecretKey aesKey = new SecretKeySpec(originKeyStr.getBytes(StandardCharsets.UTF_8), "AES"); byte[] key = aesKey.getEncoded(); //构建 aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key); } //加密 byte[] encrypt = aes.encrypt(str.getBytes(StandardCharsets.UTF_8)); String s = Base64.getUrlEncoder().encodeToString(encrypt); return s; }catch (Exception e){ e.printStackTrace(); } return ""; } // 解码并解密字符串 public static String decryptURLDecodeStr(String encStr) { try { if (aes == null) { SecretKey aesKey = new SecretKeySpec(originKeyStr.getBytes(StandardCharsets.UTF_8), "AES"); byte[] key = aesKey.getEncoded(); //构建 aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key); } //加密 byte[] bytes = Base64.getUrlDecoder().decode(encStr); String s = aes.decryptStr(bytes); return s; }catch (Exception e){ e.printStackTrace(); } return ""; } // 加密并上传文件 @SneakyThrows public static void encryptUpload(MinioClient minioClient, String bucketName, String objectName, InputStream inputStream) { if (aes == null) { SecretKey aesKey = new SecretKeySpec(originKeyStr.getBytes(StandardCharsets.UTF_8), "AES"); byte[] key = aesKey.getEncoded(); //构建 aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key); } byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes, 0, bytes.length); //加密 byte[] encrypt = aes.encrypt(bytes); ByteArrayInputStream bais = new ByteArrayInputStream(encrypt); PutObjectOptions putObjectOptions = new PutObjectOptions(bais.available(), bais.available()< 5*1024*1024 ? 5*1024*1024 : bais.available()*8); minioClient.putObject(bucketName, objectName, bais, putObjectOptions); } // 下载文件并解密 @SneakyThrows public static byte[] decryptDownload(MinioClient minioClient, String bucketName, String objectName) { if (aes == null) { SecretKey aesKey = new SecretKeySpec(originKeyStr.getBytes(StandardCharsets.UTF_8), "AES"); byte[] key = aesKey.getEncoded(); aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key); } InputStream inputStream = minioClient.getObject(bucketName, objectName); byte[] decrypt = aes.decrypt(inputStream); return decrypt; } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/11 9:22:44

激光雷达(一):原理和评价指标

前言 激光雷达——在智能驾驶领域已经不再是陌生的传感器&#xff0c;被大部分车企和智能驾驶公司看作“实现高级别自动驾驶的必要传感器”。因为&#xff0c;其特有的3维感知信息&#xff0c;可以弥补摄像头等其它传感器在极特殊场景下的感知盲区———补盲。激光雷达火起来的…

作者头像 李华
网站建设 2026/6/11 9:22:42

SolidWorks二次开发实战:用C# API一键获取界面语言,搞定多语言菜单适配

SolidWorks二次开发实战&#xff1a;用C# API构建智能多语言适配系统 在全球化设计协作的今天&#xff0c;一款专业的SolidWorks插件如果不能自动适配用户界面语言&#xff0c;就像带着翻译器参加国际会议——功能再强大也会让用户体验大打折扣。想象一下&#xff0c;德国工程师…

作者头像 李华
网站建设 2026/6/11 9:22:37

树莓派网络自治指南:实现开机自连与断网自愈

1. 树莓派网络自治的核心价值 家里放个树莓派当服务器用&#xff0c;最怕的就是半夜断网没人管。我去年做智能家居中枢时就吃过这个亏——凌晨三点空调突然失控&#xff0c;爬起来一看是树莓派掉线了。这种7x24小时运行的设备&#xff0c;网络自治能力不是锦上添花&#xff0c;…

作者头像 李华
网站建设 2026/6/11 9:22:35

技术架构解析:Sunshine如何实现跨平台自托管游戏串流

技术架构解析&#xff1a;Sunshine如何实现跨平台自托管游戏串流 【免费下载链接】Sunshine Self-hosted game stream host for Moonlight. 项目地址: https://gitcode.com/GitHub_Trending/su/Sunshine Sunshine是一个开源的自托管游戏串流服务器&#xff0c;专为Moonl…

作者头像 李华
网站建设 2026/6/11 9:22:32

用Python+MediaPipe 0.8.3实现手势控制PPT翻页(保姆级教程)

用PythonMediaPipe打造无接触PPT控制系统&#xff1a;从手势识别到办公自动化实战想象一下&#xff0c;站在会议室前方&#xff0c;无需触碰任何设备&#xff0c;仅凭几个简单手势就能流畅切换幻灯片——这种未来感十足的交互方式&#xff0c;现在用PythonMediaPipe就能轻松实现…

作者头像 李华
网站建设 2026/6/11 9:22:30

YOLOv5网络拆解:深入C3、SPP模块的设计思想与PyTorch实现避坑指南

YOLOv5核心模块工程实践&#xff1a;从C3结构解析到高效PyTorch实现 在计算机视觉领域&#xff0c;YOLOv5凭借其出色的实时检测性能成为工业界宠儿。但许多开发者在复现或修改模型时&#xff0c;往往被其复杂的模块设计所困扰——特别是那些看似简单却暗藏玄机的组件&#xff0…

作者头像 李华