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内置的日志框架,开发者只负责调用对…...
浅谈 React Hooks
React Hooks 是 React 16.8 引入的一组 API,用于在函数组件中使用 state 和其他 React 特性(例如生命周期方法、context 等)。Hooks 通过简洁的函数接口,解决了状态与 UI 的高度解耦,通过函数式编程范式实现更灵活 Rea…...
19c补丁后oracle属主变化,导致不能识别磁盘组
补丁后服务器重启,数据库再次无法启动 ORA01017: invalid username/password; logon denied Oracle 19c 在打上 19.23 或以上补丁版本后,存在与用户组权限相关的问题。具体表现为,Oracle 实例的运行用户(oracle)和集…...
手游刚开服就被攻击怎么办?如何防御DDoS?
开服初期是手游最脆弱的阶段,极易成为DDoS攻击的目标。一旦遭遇攻击,可能导致服务器瘫痪、玩家流失,甚至造成巨大经济损失。本文为开发者提供一套简洁有效的应急与防御方案,帮助快速应对并构建长期防护体系。 一、遭遇攻击的紧急应…...
Java如何权衡是使用无序的数组还是有序的数组
在 Java 中,选择有序数组还是无序数组取决于具体场景的性能需求与操作特点。以下是关键权衡因素及决策指南: ⚖️ 核心权衡维度 维度有序数组无序数组查询性能二分查找 O(log n) ✅线性扫描 O(n) ❌插入/删除需移位维护顺序 O(n) ❌直接操作尾部 O(1) ✅内存开销与无序数组相…...
【解密LSTM、GRU如何解决传统RNN梯度消失问题】
解密LSTM与GRU:如何让RNN变得更聪明? 在深度学习的世界里,循环神经网络(RNN)以其卓越的序列数据处理能力广泛应用于自然语言处理、时间序列预测等领域。然而,传统RNN存在的一个严重问题——梯度消失&#…...
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...
oracle与MySQL数据库之间数据同步的技术要点
Oracle与MySQL数据库之间的数据同步是一个涉及多个技术要点的复杂任务。由于Oracle和MySQL的架构差异,它们的数据同步要求既要保持数据的准确性和一致性,又要处理好性能问题。以下是一些主要的技术要点: 数据结构差异 数据类型差异ÿ…...
Java多线程实现之Callable接口深度解析
Java多线程实现之Callable接口深度解析 一、Callable接口概述1.1 接口定义1.2 与Runnable接口的对比1.3 Future接口与FutureTask类 二、Callable接口的基本使用方法2.1 传统方式实现Callable接口2.2 使用Lambda表达式简化Callable实现2.3 使用FutureTask类执行Callable任务 三、…...
【单片机期末】单片机系统设计
主要内容:系统状态机,系统时基,系统需求分析,系统构建,系统状态流图 一、题目要求 二、绘制系统状态流图 题目:根据上述描述绘制系统状态流图,注明状态转移条件及方向。 三、利用定时器产生时…...
Unit 1 深度强化学习简介
Deep RL Course ——Unit 1 Introduction 从理论和实践层面深入学习深度强化学习。学会使用知名的深度强化学习库,例如 Stable Baselines3、RL Baselines3 Zoo、Sample Factory 和 CleanRL。在独特的环境中训练智能体,比如 SnowballFight、Huggy the Do…...
