rain
2024-08-14 ea73b6fb3ad0b34e4d856321afecae5ada1091fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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.*;
import java.util.zip.*;
 
public class MinioFileDownloader {
 
    private final String bucketPath;
 
    public MinioFileDownloader(String bucketPath) {
        this.bucketPath = bucketPath;
    }
 
    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(time).toString();
 
        // 创建压缩文件输出流
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName))) {
            zos.setLevel(Deflater.BEST_COMPRESSION); // 使用最高压缩级别
 
            // 遍历每个前缀
            for (String prefix : prefixes) {
                // 从本地文件系统获取文件
                String localFolderPath = bucketPath + "/" + prefix;
                File localFolder = new File(localFolderPath);
                if (!localFolder.exists() || !localFolder.isDirectory()) {
                    throw new FileNotFoundException("Local folder not found: " + localFolderPath);
                }
 
                // 压缩文件夹
                zipFolder(localFolder, prefix, zos);
            }
        }
    }
 
    private void zipFolder(File folder, String parentFolder, ZipOutputStream zos) throws IOException {
        for (File file : folder.listFiles()) {
            if (file.isDirectory()) {
                zipFolder(file, parentFolder + "/" + file.getName(), zos);
                continue;
            }
            zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
            try (FileInputStream fis = new FileInputStream(file)) {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = fis.read(buffer)) >= 0) {
                    zos.write(buffer, 0, length);
                }
            }
            zos.closeEntry();
        }
    }
 
 
 
        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.");
            }
        }
 
        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();
 
                // 检查文件是否存在
                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;
            }
        }
    }