rain
2024-08-21 2db1aa88e8ab53096a936163d686b90d8e056a99
src/main/java/com/dji/sample/media/util/MinioFileDownloader.java
@@ -1,5 +1,12 @@
package com.dji.sample.media.util;
import com.dji.sample.patches.utils.TimerUtil;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.StatObjectArgs;
import io.minio.StatObjectResponse;
import io.minio.errors.MinioException;
import java.io.*;
import java.nio.file.*;
import java.util.*;
@@ -13,13 +20,13 @@
        this.bucketPath = bucketPath;
    }
    public void downloadAndZipFolders(List<String> prefixes, String localSaveDir) throws Exception {
    public void downloadAndZipFolders(List<String> prefixes, String localSaveDir,String time) throws Exception {
        // 创建目标文件夹路径并生成zip文件名
        Path localSavePath = Paths.get(localSaveDir);
        if (!Files.exists(localSavePath)) {
            Files.createDirectories(localSavePath);
        }
        String zipFileName = localSavePath.resolve("compressed_files.zip").toString();
        String zipFileName = localSavePath.resolve(time).toString();
        // 创建压缩文件输出流
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName))) {
@@ -58,23 +65,49 @@
        }
    }
    public static void main(String[] args) {
        try {
            String bucketPath = "/data/software/minio-data/cloud-bucket";
            List<String> prefixes = List.of(
                    "c88e9f12-9fab-4aca-8fc4-b02d0b0ce5f2/DJI_202407151726_001_c88e9f12-9fab-4aca-8fc4-b02d0b0ce5f2/"
            );
            MinioFileDownloader downloader = new MinioFileDownloader(bucketPath);
            // 下载并压缩文件到本地目录
            String localSaveDir = "/path/to/local/save/directory"; // 修改为你想保存的本地目录
            downloader.downloadAndZipFolders(prefixes, localSaveDir);
        public static void main(String[] args) {
            boolean success = deleteFileFromMinio("http://139.196.74.78:9000", "sxkj", "sxkj2024", "cloud-bucket", "test/瑞金图片.jpeg");
            if (!success) {
                System.out.println("File deletion failed or file did not exist.");
            }
        }
            System.out.println("压缩文件已保存到:" + localSaveDir);
        public static boolean deleteFileFromMinio(String endpoint, String accessKey, String secretKey, String bucketName, String objectName) {
            try {
                // 创建MinioClient实例
                MinioClient minioClient = MinioClient.builder()
                        .endpoint(endpoint)
                        .credentials(accessKey, secretKey)
                        .build();
        } catch (Exception e) {
            e.printStackTrace();
                // 检查文件是否存在
                try {
                    StatObjectResponse stat = minioClient.statObject(StatObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build());
                    System.out.println("File exists, proceeding with deletion.");
                    // 删除文件
                    minioClient.removeObject(RemoveObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build());
                    System.out.println("File deleted successfully");
                    return true;
                } catch (Exception e) {
                    // 如果文件不存在,则捕获异常
                    System.out.println("File does not exist or cannot be accessed: " + e.getMessage());
                    return false;
                }
            } catch (Exception e) {
                System.out.println("Unexpected error: " + e.getMessage());
                return false;
            }
        }
    }
}