Java 实现下载指定minio目录下的所有内容到远程机器
需要把minio中,指定目录下的所有文件内容,保存到远程主机中指定位置,具体实现如下:
1、添加依赖
确保项目中引入了 MinIO 的 Java SDK和远程来进行 SSH 文件上传。
<!-- MinIO SDK --><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>8.5.3</version></dependency><!-- JSch for SSH/SFTP --><dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.55</version></dependency>
2、实现方法
/*** 上传到远程主机** @param bucketName 存储桶名称* @param prefix 前缀* @param remoteHost 远程主机* @param username 用户名* @param password 密码* @param remoteDir 远程目录* @param minioClient Minio 客户端* @return {@link List }<{@link String }>*/public List<String> uploadToRemote(String bucketName, String prefix, String remoteHost, String username, String password, String remoteDir, MinioClient minioClient) throws Exception {// 连接远程主机JSch jsch = new JSch();Session session = jsch.getSession(username, remoteHost, 22);session.setPassword(password);Properties config = new Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);session.connect();ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");sftp.connect();// 确保目录存在ensureRemoteDirectory(sftp, remoteDir);Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(true).build());// 返回记录List<String> resInfo = new ArrayList<>();for (Result<Item> result : results) {Item item = result.get();String objectName = item.objectName();if (item.isDir()) continue;String relativePath = objectName.substring(prefix.length());String remoteFilePath = remoteDir + "/" + relativePath.replaceAll("^/+", "");// 创建远程子目录ensureRemoteDirectory(sftp, remoteFilePath.substring(0, remoteFilePath.lastIndexOf('/')));try (InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build())) {sftp.put(stream, remoteFilePath);resInfo.add(remoteFilePath);log.info("Uploaded: {} -> {}", objectName, remoteFilePath);}}sftp.disconnect();session.disconnect();return resInfo;}/*** 确保远程目录** @param sftp SFTP* @param remotePath 远程路径* @throws Exception 例外*/private void ensureRemoteDirectory(ChannelSftp sftp, String remotePath) throws Exception {String[] folders = remotePath.split("/");String current = "";for (String folder : folders) {if (folder.isEmpty()) continue;current += "/" + folder;try {sftp.cd(current);} catch (Exception e) {sftp.mkdir(current);}}}
3、接口调用
/*** 保存到远程计算机目录** @param bucketName 存储桶名称 minio-data* @param remotePath minio目录路径 model-file-path/data/28588882828181888/* @return {@link ResponseEntity }<{@link ? }>*/@PostMapping("/saveToRemoteMachineDirectory")public ResponseEntity<?> saveToRemoteMachineDirectory(@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 remoteDir = "/home/data/temp/" + lastLevelContents;// 调用方法List<String> result = uploadToRemote(bucketName, remotePath, "192.168.10.88", "root", "123456", remoteDir, 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中指定目录下的所有文件内容到远程指定的机器了。
上传本机的可参考:Java 实现下载指定minio目录下的所有内容到本机-CSDN博客