minio分布式存储系统
目录
拉取docker镜像
minio所需要的依赖
文件存放的位置
手动上传文件到minio中
工具类上传
yml配置
config类
service类
启动类
测试类
图片
视频
删除minio服务器的文件
下载minio服务器的文件
拉取docker镜像
拉取稳定版本:docker pull minio/minio:RELEASE.2021-06-17T00-10-46Z-28-gac7697426创建并运行容器: docker run -d -p 9000:9000 --name minio \-e "MINIO_ACCESS_KEY=minio" \-e "MINIO_SECRET_KEY=minio123" \-v /path/to/data:/data \-v /path/to/config:/root/.minio \minio/minio:RELEASE.2021-06-17T00-10-46Z server /data访问minmo系统: http://192.168.74.128:9000
minio所需要的依赖
<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>7.1.0</version></dependency>
文件存放的位置
退出minio容器
exit
手动上传文件到minio中
public static void main(String[] args) {FileInputStream fileInputStream = null;try {fileInputStream = new FileInputStream("e:\\index.js");;//1.创建minio链接客户端MinioClient minioClient = MinioClient.builder().credentials("minio", "minio123").endpoint("http://192.168.74.128:9000").build();//2.上传PutObjectArgs putObjectArgs = PutObjectArgs.builder().object("plugins/js/index.js")//文件名.contentType("text/js")//文件类型.bucket("leadnews")//桶名词 与minio创建的名词一致.stream(fileInputStream, fileInputStream.available(), -1) //文件流.build();minioClient.putObject(putObjectArgs);} catch (Exception ex) {ex.printStackTrace();}}
工具类上传
yml配置
minio:accessKey: miniosecretKey: minio123bucket: leadnewsendpoint: http://192.168.74.128:9000readPath: http://192.168.74.128:9000
config类
package com.heima.file.config;import com.heima.file.service.FileStorageService;
import io.minio.MinioClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Data
@Configuration
@EnableConfigurationProperties({MinIOConfigProperties.class})
//当引入FileStorageService接口时
@ConditionalOnClass(FileStorageService.class)
public class MinIOConfig {@Autowiredprivate MinIOConfigProperties minIOConfigProperties;@Beanpublic MinioClient buildMinioClient() {return MinioClient.builder().credentials(minIOConfigProperties.getAccessKey(), minIOConfigProperties.getSecretKey()).endpoint(minIOConfigProperties.getEndpoint()).build();}
}
package com.ma.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;import java.io.Serializable;@Data
@ConfigurationProperties(prefix = "minio") // 文件上传 配置前缀file.oss
public class MinIOConfigProperties implements Serializable {private String accessKey;private String secretKey;private String bucket;private String endpoint;private String readPath;
}
service类
package com.ma.service;import java.io.InputStream;/*** @author itheima*/
public interface FileStorageService {/*** 上传图片文件* @param prefix 文件前缀* @param filename 文件名* @param inputStream 文件流* @return 文件全路径*/public String uploadImgFile(String prefix, String filename,InputStream inputStream);/*** 上传html文件* @param prefix 文件前缀* @param filename 文件名* @param inputStream 文件流* @return 文件全路径*/public String uploadHtmlFile(String prefix, String filename,InputStream inputStream);/*** 删除文件* @param pathUrl 文件全路径*/public void delete(String pathUrl);/*** 下载文件* @param pathUrl 文件全路径* @return**/public byte[] downLoadFile(String pathUrl);}
package com.ma.service.impl;import com.ma.config.MinIOConfig;
import com.ma.config.MinIOConfigProperties;
import com.ma.service.FileStorageService;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;@Slf4j
@EnableConfigurationProperties(MinIOConfigProperties.class)
@Import(MinIOConfig.class)
@Service
public class MinIOFileStorageService implements FileStorageService {@Autowiredprivate MinioClient minioClient;@Autowiredprivate MinIOConfigProperties minIOConfigProperties;private final static String separator = "/";/*** @param dirPath* @param filename yyyy/mm/dd/file.jpg* @return*/public String builderFilePath(String dirPath,String filename) {StringBuilder stringBuilder = new StringBuilder(50);if(!StringUtils.isEmpty(dirPath)){stringBuilder.append(dirPath).append(separator);}SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");String todayStr = sdf.format(new Date());stringBuilder.append(todayStr).append(separator);stringBuilder.append(filename);return stringBuilder.toString();}/*** 上传图片文件* @param prefix 文件前缀* @param filename 文件名* @param inputStream 文件流* @return 文件全路径*/@Overridepublic String uploadImgFile(String prefix, String filename,InputStream inputStream) {String filePath = builderFilePath(prefix, filename);try {PutObjectArgs putObjectArgs = PutObjectArgs.builder().object(filePath).contentType("image/jpg").bucket(minIOConfigProperties.getBucket()).stream(inputStream,inputStream.available(),-1).build();minioClient.putObject(putObjectArgs);StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());urlPath.append(separator+minIOConfigProperties.getBucket());urlPath.append(separator);urlPath.append(filePath);return urlPath.toString();}catch (Exception ex){log.error("minio put file error.",ex);throw new RuntimeException("上传文件失败");}}/*** 上传html文件* @param prefix 文件前缀* @param filename 文件名* @param inputStream 文件流* @return 文件全路径*/@Overridepublic String uploadHtmlFile(String prefix, String filename,InputStream inputStream) {String filePath = builderFilePath(prefix, filename);try {PutObjectArgs putObjectArgs = PutObjectArgs.builder().object(filePath).contentType("text/html").bucket(minIOConfigProperties.getBucket()).stream(inputStream,inputStream.available(),-1).build();minioClient.putObject(putObjectArgs);StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());urlPath.append(separator+minIOConfigProperties.getBucket());urlPath.append(separator);urlPath.append(filePath);return urlPath.toString();}catch (Exception ex){log.error("minio put file error.",ex);ex.printStackTrace();throw new RuntimeException("上传文件失败");}}/*** 删除文件* @param pathUrl 文件全路径*/@Overridepublic void delete(String pathUrl) {String key = pathUrl.replace(minIOConfigProperties.getEndpoint()+"/","");int index = key.indexOf(separator);String bucket = key.substring(0,index);String filePath = key.substring(index+1);// 删除ObjectsRemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucket).object(filePath).build();try {minioClient.removeObject(removeObjectArgs);} catch (Exception e) {log.error("minio remove file error. pathUrl:{}",pathUrl);e.printStackTrace();}}/*** 下载文件* @param pathUrl 文件全路径* @return 文件流**/@Overridepublic byte[] downLoadFile(String pathUrl) {String key = pathUrl.replace(minIOConfigProperties.getEndpoint()+"/","");int index = key.indexOf(separator);String bucket = key.substring(0,index);String filePath = key.substring(index+1);InputStream inputStream = null;try {inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(minIOConfigProperties.getBucket()).object(filePath).build());} catch (Exception e) {log.error("minio down file error. pathUrl:{}",pathUrl);e.printStackTrace();}ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();byte[] buff = new byte[100];int rc = 0;while (true) {try {if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;} catch (IOException e) {e.printStackTrace();}byteArrayOutputStream.write(buff, 0, rc);}return byteArrayOutputStream.toByteArray();}
}
启动类
@SpringBootApplication@ComponentScan(basePackages = {"com.ma.config", "com.ma.service"})
public class SpringBootTest01Application {public static void main(String[] args) {SpringApplication.run(SpringBootTest01Application.class, args);}
}
测试类
图片
package com.ma.springboottest01;import com.ma.service.FileStorageService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.io.FileInputStream;
import java.io.InputStream;@SpringBootTest
class SpringBootTest01ApplicationTests {@Autowiredprivate FileStorageService fileStorageService;@Testvoid contextLoads() {// 示例用法try {// 创建一个InputStream,例如从本地文件获取InputStream fileInputStream = new FileInputStream("D:\\Image\\星空.jpg");// 调用uploadImgFile方法进行文件上传String prefix = "user_images/"; // 设置文件前缀String filename = "unique_image_name.jpg"; // 设置文件名
// String url = uploadImgFile(prefix, filename, fileInputStream);// url现在包含了上传文件的访问路径String url = fileStorageService.uploadImgFile(prefix, filename, fileInputStream);System.out.println("File uploaded successfully. URL: " + url);} catch (Exception e) {// 处理上传失败的情况e.printStackTrace();}}
}
视频
package com.ma.springboottest01;import com.ma.service.FileStorageService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.io.FileInputStream;
import java.io.InputStream;@SpringBootTest
class SpringBootTest01ApplicationTests {@Autowiredprivate FileStorageService fileStorageService;@Testvoid contextLoads() {// 示例用法try {// 创建一个InputStream,例如从本地文件获取
// InputStream fileInputStream = new FileInputStream("D:\\Image\\星空.jpg");InputStream fileInputStream = new FileInputStream("D:\\video\\test.mp4");// 调用uploadImgFile方法进行文件上传// 设置文件前缀和文件名String prefix = "videos/";String filename = "example_video.mp4";
// String url = uploadImgFile(prefix, filename, fileInputStream);// url现在包含了上传文件的访问路径String url = fileStorageService.uploadImgFile(prefix, filename, fileInputStream);System.out.println("File uploaded successfully. URL: " + url);} catch (Exception e) {// 处理上传失败的情况e.printStackTrace();}}
}
删除minio服务器的文件
@Testvoid t2(){fileStorageService.delete("http://192.168.74.128:9000/leadnews/videos//2023/11/27/example_video.mp4");}
下载minio服务器的文件
@Testvoid t2() {byte[] bytes = fileStorageService.downLoadFile("http://192.168.74.128:9000/leadnews/user_images//2023/11/27/unique_image_name.jpg");// 保存到本地文件的示例String localFilePath = "E:guo.jpg//"; // 替换为实际的本地文件路径saveToFile(bytes, localFilePath);}public static void saveToFile(byte[] fileContent, String localFilePath) {try (FileOutputStream fos = new FileOutputStream(localFilePath)) {fos.write(fileContent);System.out.println("File saved locally: " + localFilePath);} catch (IOException e) {e.printStackTrace();}}
相关文章:

minio分布式存储系统
目录 拉取docker镜像 minio所需要的依赖 文件存放的位置 手动上传文件到minio中 工具类上传 yml配置 config类 service类 启动类 测试类 图片 视频 删除minio服务器的文件 下载minio服务器的文件 拉取docker镜像 拉取稳定版本:docker pull minio/minio:RELEASE.20…...

Kafka 如何保证消息消费的全局顺序性
哈喽大家好,我是咸鱼 今天我们继续来讲一讲 Kafka 当有消息被生产出来的时候,如果没有指定分区或者指定 key ,那么消费会按照【轮询】的方式均匀地分配到所有可用分区中,但不一定按照分区顺序来分配 我们知道,在 Kaf…...
boa+cgi上传文件超过1M报错问题
写在前面 今天需要使用页面上传bin包,文件大概是3.9mb,结果一直报错 POST /cgi-bin/Upgrade.cgi undefined Host: 192.168.137.200:8888 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0 Accept: text/h…...

抖去推--短视频账号矩阵系统saas工具源码技术开发(源头)
一、短视频矩阵系统搭建常见问题? 1、抖去推的短视频AI矩阵营销软件需要一定的技术水平吗? 答:不需要。产品简单易用,不需要具备专业的技术水平,即使是初学者,也能够轻松上手操作。 3、抖去推的短视频AI矩…...

【服务器能干什么】搭建一个短网址平台,可以查看数据详情!
昨天在 YouTube 上看到又一个搭建自己短网址的视频教程,用的是开源的 polr,但是按照步骤一步步搭建下来,最后一步都会出现 顺哥轻创 PLAINTEXT Whoops, looks like something went wrong百度、谷歌查了一圈也没找到有效的解决方法。&#x…...

MySQL备份与恢复(重点)
MySQL备份与恢复(重点) 一、用户管理与权限管理 ☆ 用户管理 1、创建MySQL用户 注意:MySQL中不能单纯通过用户名来说明用户,必须要加上主机。如jack10.1.1.1 基本语法: mysql> create user 用户名被允许连接的主…...

机器学习中的特征选择:方法和 Python 示例
布拉加德什桑达拉拉詹 一、说明 特征选择是机器学习流程中至关重要且经常被低估的步骤。它涉及从数据集中的原始特征集中选择最相关的特征(输入变量或属性)的子集。特征选择的重要性怎么强调都不为过,因为它直接影响机器学习模型的质量、效率…...

有哪些不错的golang开源项目?
前言 下面是github上的golang项目,适合练手,可以自己选择一些项目去练习,整理不易,希望能多多点赞收藏一下!废话少说,我们直接进入正题>>> 先推荐几个教程性质的项目(用于新手学习、…...

解决ssh使用public key远程登录服务器拒绝问题
目录 使用场景windows安装ssh客户端使用powershell ssh登录服务器生成密钥文件ubuntu ssh服务器配置使用vscode远程登录使用Xshell远程登录使用MobaXtem远程登录Server refused our key问题解决方案 使用场景 使用vscode远程ssh登录使用public key不需要输入密码,比较方便. w…...

js数组中,相同id的item数据合并
原数据: const list [ {id:1, key: a}, {id:1, key: b}, {id:2, key: c}, {id:2, key: d}, ]期望数据格式 const newList [ {id:1, keyList: [a,b]}, {id:2, keyList: [c,d]}, ]// 相同id的数据合并let newList_(list ).flatten().groupBy(id).map(_.spread((..…...

LeetCode(33)最小覆盖子串【滑动窗口】【困难】
目录 1.题目2.答案3.提交结果截图 链接: 76. 最小覆盖子串 1.题目 给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。 注意: 对于 t 中重复字…...
设计模式 创建者模式
设计模式 创建者模式 前言原来代码使用设计模式总结Builder模式在源码中的应用:其他代码 前言 “对象创建”模式——绕开new 工厂模式 抽象工厂 原型模式 构建器 动机与背景 目前需要建造一个房子,建造房子需要一系列特定的步骤,但是房子的类…...

排序算法--插入排序
实现逻辑 ① 从第一个元素开始,该元素可以认为已经被排序 ② 取出下一个元素,在已经排序的元素序列中从后向前扫描 ③如果该元素(已排序)大于新元素,将该元素移到下一位置 ④ 重复步骤③,直到找到已排序的元…...

【操作宝典】SQL巨擘:掌握SQL Server Management的终极秘籍!
目录 ⛳️【SQL Server Management】 ⛳️1. 启动登录 ⛳️2. 忘记密码 ⛳️3. 操作数据库和表 3.1 新建数据库text 3.2 新建表 3.3 编辑表 3.4 编写脚本 ⛳️【SQL Server Management】 ⛳️1. 启动登录 需要开启服务 ⛳️2. 忘记密码 登录windows--> 安全性 -->…...

Airtest遇到模拟器无法输入中文的情况该如何处理?
1. 前言 最近有收到同学们的一些提问,使用Airtest的 text 接口,发现在部分模拟器上, text 无法输入中文,不知道该怎么处理。 今天我们就输入这个小问题,来详细聊一下。 2. Airtest的输入法简介 对于Android设备来说…...
从农夫山泉家族任命,看“食企二代”的接班与传承
本文转载自产业科技 农夫山泉再次引发舆论关注,起因是一则人事任命消息。 市场消息称,农夫山泉对区域及人员进行了调整,其总部所在地浙江省被划分为四个区域,在以往浙南、浙北基础上多了浙西大区以及杭州大区,其中农…...
JavaScript启动本地应用程序
JavaScript调起本地应用程序 以下内容,自定义部分我也还未经过实际验证,酌情查看。 文章目录 JavaScript调起本地应用程序确定协议调用协议传参自定义写入协议获取参数 在浏览器中通过 JavaScript调起本地应用程序的一个可行方法就是 通过协议调起。 …...
软件工程理论与实践 (吕云翔)第十四章 软件维护与软件工程管理课后习题与解析
第十四章 软件维护与软件工程管理 1.判断题 (1)代码行技术是比较简单的定量估算软件规模的方法。(√) (2)功能点技术依据对软件信息域特性和软件复杂性的评估结果,估算软件规模。(√) &#…...

Flutter 桌面应用开发之读写Windows注册表
文章目录 需求来源Windows查询Windows版本号方法1. 如何查看Windows版本号2. Windows开发如何通过代码查询Windows版本号(1) 使用C#代码:(2) 使用VB.NET代码 3.通过注册表查看Windows版本信息 Flutter查询Windows版本号方法依赖库支持平台实现步骤1. 在pubspec.yaml…...

【Java Spring】SpringBoot 日志系统
文章目录 一、Spring Boot 日志系统1.1 Spring Boot 日志框架1.2 自定义日志打印1.3 日志级别设置1.4 日志持久化1.5 lombok 简化日志输出 一、Spring Boot 日志系统 1.1 Spring Boot 日志框架 SLF4J 和 logback都是spring boot内置的日志框架,开发者只负责调用对…...

SpringBoot-17-MyBatis动态SQL标签之常用标签
文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…...
web vue 项目 Docker化部署
Web 项目 Docker 化部署详细教程 目录 Web 项目 Docker 化部署概述Dockerfile 详解 构建阶段生产阶段 构建和运行 Docker 镜像 1. Web 项目 Docker 化部署概述 Docker 化部署的主要步骤分为以下几个阶段: 构建阶段(Build Stage):…...
FFmpeg 低延迟同屏方案
引言 在实时互动需求激增的当下,无论是在线教育中的师生同屏演示、远程办公的屏幕共享协作,还是游戏直播的画面实时传输,低延迟同屏已成为保障用户体验的核心指标。FFmpeg 作为一款功能强大的多媒体框架,凭借其灵活的编解码、数据…...
【算法训练营Day07】字符串part1
文章目录 反转字符串反转字符串II替换数字 反转字符串 题目链接:344. 反转字符串 双指针法,两个指针的元素直接调转即可 class Solution {public void reverseString(char[] s) {int head 0;int end s.length - 1;while(head < end) {char temp …...
【学习笔记】深入理解Java虚拟机学习笔记——第4章 虚拟机性能监控,故障处理工具
第2章 虚拟机性能监控,故障处理工具 4.1 概述 略 4.2 基础故障处理工具 4.2.1 jps:虚拟机进程状况工具 命令:jps [options] [hostid] 功能:本地虚拟机进程显示进程ID(与ps相同),可同时显示主类&#x…...
CMake控制VS2022项目文件分组
我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...

NXP S32K146 T-Box 携手 SD NAND(贴片式TF卡):驱动汽车智能革新的黄金组合
在汽车智能化的汹涌浪潮中,车辆不再仅仅是传统的交通工具,而是逐步演变为高度智能的移动终端。这一转变的核心支撑,来自于车内关键技术的深度融合与协同创新。车载远程信息处理盒(T-Box)方案:NXP S32K146 与…...

C/C++ 中附加包含目录、附加库目录与附加依赖项详解
在 C/C 编程的编译和链接过程中,附加包含目录、附加库目录和附加依赖项是三个至关重要的设置,它们相互配合,确保程序能够正确引用外部资源并顺利构建。虽然在学习过程中,这些概念容易让人混淆,但深入理解它们的作用和联…...

基于Springboot+Vue的办公管理系统
角色: 管理员、员工 技术: 后端: SpringBoot, Vue2, MySQL, Mybatis-Plus 前端: Vue2, Element-UI, Axios, Echarts, Vue-Router 核心功能: 该办公管理系统是一个综合性的企业内部管理平台,旨在提升企业运营效率和员工管理水…...

数学建模-滑翔伞伞翼面积的设计,运动状态计算和优化 !
我们考虑滑翔伞的伞翼面积设计问题以及运动状态描述。滑翔伞的性能主要取决于伞翼面积、气动特性以及飞行员的重量。我们的目标是建立数学模型来描述滑翔伞的运动状态,并优化伞翼面积的设计。 一、问题分析 滑翔伞在飞行过程中受到重力、升力和阻力的作用。升力和阻力与伞翼面…...