文章目录
需求
需要批量下载一个bucket下的内容,bucket下文件有19GB+,且文件夹结构复杂,使用官方的Console无法完成这么大量文件的下载,而且也不支持文件夹的分享,所以自己写个工具下载,顺便把上传的也写了。
使用官方的打包下载,由于文件太多,一直转,而且session一失效,或者刷新页面,就得重来!
所以官方这个下载,只适合少量文件的打包下载。
批量下载
代码
核心逻辑就是递归,遇到文件夹就继续找文件,遇到文件就下载。
package cn.xdf.xadd.rmq.test;import io.minio.GetObjectArgs;import io.minio.ListObjectsArgs;import io.minio.MinioClient;import io.minio.Result;import io.minio.messages.Item;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.InputStream;public class BatchDownloadUtil { private static final String ENDPOINT = "http://minio:80"; private static final String ACCESS_KEY = "ak"; private static final String SECRET_KEY = "sk"; private static final String BUCKET_NAME = "milvus-t-backup-all"; private static final String ROOT_PATH = "files/tiku_backup/"; private static final String LOCAL_DIR = "/Users/xdf/Downloads"; static MinioClient minioClient = MinioClient.builder().endpoint(ENDPOINT).credentials(ACCESS_KEY, SECRET_KEY).build(); public static void main(String[] args) throws Exception { ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder().bucket(BUCKET_NAME).prefix(ROOT_PATH).build(); findAndSaveFile(listObjectsArgs); } private static void findAndSaveFile(ListObjectsArgs listObjectsArgs) throws Exception { // List all objects in the bucket Iterable<Result<Item>> results = minioClient.listObjects(listObjectsArgs); for (Result<Item> result : results) { Item item = result.get(); if (item.isDir()) { System.out.println("文件夹:" + item.objectName()); ListObjectsArgs args = ListObjectsArgs.builder().bucket(BUCKET_NAME).prefix(item.objectName()).build(); findAndSaveFile(args); } else { GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(BUCKET_NAME).object(item.objectName()).build(); String objectName = item.objectName(); // Create a local file with the same name as the object File file = new File(LOCAL_DIR + File.separator + objectName); // Create parent directories if needed file.getParentFile().mkdirs(); // Get the object as an input stream try (InputStream stream = minioClient.getObject(getObjectArgs)) { // Copy the input stream to the file FileUtils.copyInputStreamToFile(stream, file); } System.out.printf("文件:%s 下载成功!\n", item.objectName()); } } }}
效果
批量上传
代码
package cn.xdf.xadd.rmq.test;import io.minio.MinioClient;import io.minio.UploadObjectArgs;import java.io.File;public class BatchUploadUtil { private static final String ENDPOINT = "http://minio:80"; private static final String ACCESS_KEY = "ak"; private static final String SECRET_KEY = "sk"; private static final String BUCKET_NAME = "milvus-chatglm"; private static final String PARENT_PATH = "/Users/xdf/Desktop/Temp/"; private static final String FILE_PATH = "audio"; private static final String IGNORE_FILE = ".DS_Store"; static MinioClient minioClient = MinioClient.builder().endpoint(ENDPOINT).credentials(ACCESS_KEY, SECRET_KEY).build(); public static void main(String[] args) throws Exception { // 创建一个 File 对象,指定要遍历的目录的路径 File dir = new File(PARENT_PATH + FILE_PATH); listFileAndUpload(dir); } public static void listFileAndUpload(File dir) throws Exception { // 调用 listFiles 方法获取该目录中的所有文件 File[] files = dir.listFiles(); // 遍历文件数组 for (File file : files) { // 判断是否为文件,如果是就打印出绝对路径 if (file.isFile()) { if (!file.getName().equals(IGNORE_FILE)) { uploadFile(file.getAbsolutePath()); System.out.printf("文件:%s 上传完成!\n", file.getAbsolutePath()); } } else { System.out.println("文件夹:" + file.getAbsolutePath()); // 如果是目录,就递归调用 listFiles 方法,继续遍历子目录 listFileAndUpload(file); } } } private static void uploadFile(String filePath) throws Exception { UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder().bucket(BUCKET_NAME) .object(filePath.substring(PARENT_PATH.length())) .filename(filePath) .build(); minioClient.uploadObject(uploadObjectArgs); }}
效果
上传后,保留了文件的目录结构
完工!
来源地址:https://blog.csdn.net/sinat_14840559/article/details/131852253