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

minio上传文件

minio上传文件

  • 添加依赖
  • 配置minio客户端
  • 创建服务类处理文件上传
  • application.yml配置文件

添加依赖

<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.5.5</version>
</dependency>

配置minio客户端

创建配置类,注入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.endpoint}")private String endpoint;@Value("${minio.accessKey}")private String accessKey;@Value("${minio.secretKey}")private String secretKey;@Value("${minio.bucketName}")private String bucketName;@Beanpublic MinioClient minioClient() {return MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();}
}

创建服务类处理文件上传

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.PutObjectArgs;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.io.InputStream;
import java.util.UUID;@Service
public class MinioService {private final MinioClient minioClient;@Value("${minio.bucketName}")private String bucketName;@Autowiredpublic MinioService(MinioClient minioClient) {this.minioClient = minioClient;}// 上传文件到MinIOpublic String uploadFile(MultipartFile file) throws Exception {// 检查存储桶是否存在,不存在则创建if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());}// 生成唯一文件名,避免冲突String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();// 上传文件try (InputStream inputStream = file.getInputStream()) {minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, file.getSize(), -1).contentType(file.getContentType()).build());}// 返回文件访问URL(根据MinIO配置生成)return "/api/files/" + fileName; // 自定义API路径,用于后续文件访问}
}

application.yml配置文件

minio:endpoint: http://localhost:9000accessKey: your-access-keysecretKey: your-secret-keybucketName: your-bucket-name# 可选:服务器配置
server:port: 8080servlet:context-path: /api

相关文章:

  • 小番茄C盘清理:专业高效的电脑磁盘清理工具
  • 基于算法竞赛的c++编程(23)原码,反码,补码
  • LLMs之MCP:《Evaluation Report on MCP Servers》翻译与解读
  • comfyui 工作流中 图生视频 如何增加视频的长度到5秒
  • CppCon 2015 学习:Large Scale C++ With Modules
  • Oracle实用参考(13)——Oracle for Linux物理DG环境搭建(2)
  • 0x-2-Oracle Linux 9上安装JDK配置环境变量
  • RISC-V 开发板 + Ubuntu 23.04 部署 open_vins 过程
  • 03.数据类型
  • 【读论文】OpenAI o3与o4系统模型技术报告解读
  • 基于机器学习的智能故障预测系统:构建与优化
  • Go语言--语法基础5--基本数据类型--输入输出(1)
  • 计算机常用快捷键分类汇总,涵盖 Windows、macOS 以及通用软件场景
  • 20242817李臻-安全文件传输系统-项目验收
  • Android 集成 Firebase 指南
  • Alight Motion汉化版:视频剪辑,轻松上手
  • 基于安卓的文件管理器程序开发研究源码数据库文档
  • surfer15安装
  • web架构4------(nginx常用变量,nginx中英文自动匹配,lnmp网站架构,正向代理,反向代理,负载均衡)
  • 力扣面试150题--课程表
  • 怎么什么软件可以吧做网站/天津网站策划
  • 购物网站源码/蜜雪冰城推广软文
  • 做gif表情包的网站/百度登录个人中心官网
  • 华星建设集团网站/网络营销期末考试试题及答案
  • 企业网站源码库/怎么提交百度收录
  • 南宁手机平台网网站建设/宜昌网站建设公司