liusuyi
2025-09-06 bb508127e9454e8b39ac7d431765b6de0ac74c35
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.ard.tianyu.component.minio.utils;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
 
import com.ard.common.utils.ServletUtils;
import com.ard.common.utils.spring.SpringUtils;
import io.minio.*;
import org.springframework.web.multipart.MultipartFile;
import io.minio.http.Method;
 
/**
 * Minio 文件存储工具类
 *
 * @author ruoyi
 */
public class MinioUtil {
    /**
     * 判断桶是否存在
     *
     * @param bucketName 桶名称
     * @return 如果桶存在返回 true,否则返回 false
     * @throws IOException
     */
    public static boolean bucketExists(String bucketName) throws IOException {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try {
            return minioClient.bucketExists(io.minio.BucketExistsArgs.builder().bucket(bucketName).build());
        } catch (Exception e) {
            throw new IOException("Error checking if bucket exists", e);
        }
    }
 
    // 创建桶
    public static void createBucket(String bucketName) throws IOException {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try {
            minioClient.makeBucket(io.minio.MakeBucketArgs.builder().bucket(bucketName).build());
            setBucketPolicy(bucketName);
        } catch (Exception e) {
            throw new IOException("Error creating bucket", e);
        }
    }
 
 
    /**
     * 设置桶策略
     * 刘苏义
     * 2023/9/20 10:01:39
     */
    public static boolean setBucketPolicy(String bucket) throws Exception {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try {
            //设置策略
            String sb = "{\"Version\":\"2012-10-17\"," +
                    "\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":" +
                    "{\"AWS\":[\"*\"]},\"Action\":[\"s3:ListBucket\",\"s3:ListBucketMultipartUploads\"," +
                    "\"s3:GetBucketLocation\"],\"Resource\":[\"arn:aws:s3:::" + bucket +
                    "\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:PutObject\",\"s3:AbortMultipartUpload\",\"s3:DeleteObject\",\"s3:GetObject\",\"s3:ListMultipartUploadParts\"],\"Resource\":[\"arn:aws:s3:::" +
                    bucket + "/*\"]}]}";
            SetBucketPolicyArgs setBucketPolicyArgs = SetBucketPolicyArgs.builder()
                    .bucket(bucket)
                    .config(sb)
                    .build();
            minioClient.setBucketPolicy(setBucketPolicyArgs);
            return true;
        } catch (Exception ex) {
            throw new Exception("设置桶策略异常:" + ex.getMessage());
        }
    }
 
 
    /**
     * 上传文件
     *
     * @param bucketName 桶名称
     * @param fileName
     * @throws IOException
     */
    public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException {
        String url = "";
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream inputStream = multipartFile.getInputStream()) {
            minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream,
                    multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build());
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build());
            url = url.substring(0, url.indexOf('?'));
            return ServletUtils.urlDecode(url);
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }
 
    /**
     * 上传本地文件
     *
     * @param bucketName    桶名称
     * @param fileName      存储到 Minio 的文件名
     * @param localFilePath 本地文件绝对路径
     * @return 访问 URL
     * @throws IOException
     */
    public static String uploadLocalFile(String bucketName, String fileName, String localFilePath) throws IOException {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream inputStream = new FileInputStream(localFilePath)) {
            File file = new File(localFilePath);
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .stream(inputStream, file.length(), -1)
                            .contentType("application/octet-stream") // 可根据需求自行修改
                            .build()
            );
            String url = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .method(Method.GET)
                            .build()
            );
            return ServletUtils.urlDecode(url.substring(0, url.indexOf('?')));
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }
 
    public static String uploadInputStream(String bucketName, String fileName, InputStream inputStream, long size,
                                           String contentType) throws IOException {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        if (!bucketExists(bucketName)) {
            createBucket(bucketName);
        }
        try {
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .stream(inputStream, size, -1)
                            .contentType(contentType)
                            .build()
            );
 
            String url = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .method(Method.GET)
                            .build()
            );
            url = url.substring(0, url.indexOf('?'));
            return ServletUtils.urlDecode(url);
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ignore) {
                }
            }
        }
    }
 
    /**
     * 判断文件是否存在
     */
    public static boolean exists(String bucketName, String fileName) throws IOException {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        if (!bucketExists(bucketName)) {
            createBucket(bucketName);
        }
        try {
            minioClient.statObject(
                    io.minio.StatObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .build()
            );
            return true;
        } catch (io.minio.errors.ErrorResponseException e) {
            if ("NoSuchKey".equals(e.errorResponse().code()) || "NoSuchObject".equals(e.errorResponse().code())) {
                return false;
            }
            throw new IOException(e.getMessage(), e);
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }
 
    /**
     * 删除文件
     */
    public static void delete(String bucketName, String fileName) throws IOException {
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try {
            minioClient.removeObject(
                    io.minio.RemoveObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .build()
            );
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }
 
}