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

Java 实现下载指定minio目录下的所有内容到本机

需要把minio中,指定目录下的所有文件保存到本机指定位置,具体实现方法如下:

1、添加依赖

确保项目中引入了 MinIO 的 Java SDK

<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.4.3</version> <!-- 请根据最新版本调整 -->
</dependency>

2、实现方法

/*** 下载 Minio Directory 到本地目录** @param bucketName     存储桶名称* @param directoryPath  directory path (目录路径)* @param localDirectory 本地目录* @param minioClient    Minio 客户端* @return {@link List }<{@link String }>*/public static List<String> downloadMinioDirectory(String bucketName, String directoryPath, String localDirectory, MinioClient minioClient) throws Exception {// 确保本地目录存在File localDirFile = new File(localDirectory);if (!localDirFile.exists()) {localDirFile.mkdirs();}// 获取目录下的所有对象Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(directoryPath)// 递归列出目录下的所有文件.recursive(true).build());// 返回记录List<String> resInfo = new ArrayList<>();// 遍历每个对象并下载for (Result<Item> result : results) {Item item = result.get();// 对象的键名String objectName = item.objectName();String relativePath = objectName.substring(directoryPath.length()).replaceFirst("^/", "");// 构造本地文件路径Path localFilePath = Paths.get(localDirectory, relativePath);// 创建必要的父目录Files.createDirectories(localFilePath.getParent());// 下载文件到本地minioClient.downloadObject(DownloadObjectArgs.builder().bucket(bucketName).object(objectName).filename(localFilePath.toString()).build());resInfo.add(String.valueOf(localFilePath));log.info("Downloaded: {} -> {}", objectName, localFilePath);}return resInfo;}

3、接口调用

/*** 下载当前服务机器目录 (下载到本机目录)** @param bucketName 存储桶名称 minio-data* @param remotePath 远程路径 model-file-path/data/28588882828181888/* @return {@link ResponseEntity }<{@link ? }>*/@PostMapping("/downloadCatalog")public ResponseEntity<?> downloadCatalog(@RequestParam String bucketName, @RequestParam String remotePath) {try {// 初始化 MinIO 客户端MinioClient minioClient = MinioClient.builder().endpoint(minioConfig.getEndpoint()).credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey()).build();// 使用 substring 和 lastIndexOf 方法提取 remotePath 中最后一级目录int lastSlashIndex = remotePath.lastIndexOf("/");int secondLastSlashIndex = remotePath.lastIndexOf("/", lastSlashIndex - 1);String lastLevelContents = remotePath.substring(secondLastSlashIndex + 1, lastSlashIndex);// 本地保存路径String localDirectoryPath = "/home/data/temp/" + lastLevelContents;// 调用下载方法List<String> result = downloadMinioDirectory(bucketName, remotePath, localDirectoryPath, minioClient);// 返回内容return ResponseEntity.ok(Map.of("code", 200,"data", result,"msg", "下载完成"));} catch (Exception e) {return ResponseEntity.status(500).body(Map.of("code", 500,"msg", "下载失败","error", e.getMessage()));}}

至此,即可实现下载minio中指定目录下的所有文件内容了。

http://www.xdnf.cn/news/714439.html

相关文章:

  • 深入解析注解框架实现原理:从源码到实战
  • 【下拉选项数据管理优化实践:从硬编码到高扩展性架构】
  • Jetson nx下realsense相机系统重启后找不到相机,需要重新插拔usb口问题解决办法
  • 实验设计与分析(第6版,Montgomery)第5章析因设计引导5.7节思考题5.5 R语言解题
  • 云渲染农场行业需求,如何搭建,有什么用途?
  • CDN安全加速:HTTPS加密最佳配置方案
  • C# Costura.Fody 排除多个指定dll
  • T5和GPT哪个更强大
  • C语言的函数调用,允许参数缺省和乱序
  • 通配符(Wildcard)与正则表达式(Regular Expression)的关系及区别
  • Python中re模块结合正则表达式的应用
  • 企业文件乱、传输慢?用群晖 NAS 构建安全高效的共享系统
  • Codejock ToolkitPro 与 BCGControlBar Pro 深度对比
  • 太阳系运行模拟程序-html动画
  • 宝塔安装WordPress程序
  • Rust入门之并发编程基础(一)
  • 【无标题】C++23新特性:支持打印volatile指针
  • 字节开源BAGEL可文生图、图像理解、图像编辑
  • 秒杀/高并发解决方案+落地实现
  • 【Pandas】pandas DataFrame duplicated
  • docker运行centos提示Operation not permitted
  • 快速了解 GO之接口解耦
  • 涨薪技术|0到1学会性能测试第89课-性能测试设计
  • R语言基础| 数据基本管理与操作
  • #Js篇:两个前端应用通过postMessage传递file对像
  • 02.K8S核心概念
  • JVM Full GC 频繁问题排查、优化及解决方案
  • ansible template 文件中如果包含{{}} 等非ansible 变量处理
  • git reset --hard HEAD~1与git reset --hard origin/xxx
  • CentOS_7.9 2U物理服务器上部署系统简易操作步骤