• Java使用MinIO及SpringBoot集成


    简介

    MinIO Java Client SDK提供简单的API来访问任何与Amazon S3兼容的对象存储服务。
    官方demo: https://github.com/minio/minio-java
    官方文档:https://docs.min.io/docs/java-client-api-reference.html
    普通Java集成

    第一步

    引入pom.xml依赖。

    <!-- MinIO 依赖 -->
    <dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>8.3.3</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    第二步

    测试类:

    package com.example.demo.minio;
    
    import io.minio.*;
    import io.minio.errors.MinioException;
    
    import java.io.*;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    
    /**
     * @author :Mall
     * @date :Created in 2021-11-24
     * @description :
     */
    public class FileUploader {
    
        public static void main(String[] args) {
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://127.0.0.1:9000")
                            .credentials("admin", "maluole123")
                            .build();
    
            //upload(minioClient, "asiatrip", "C:\\Users\\Administrator\\Desktop\\个人物品\\测试文件\\b.jpg", "d.jpg");
            downloadMinio(minioClient, "G://ddd.jpg", "asiatrip", "d.jpg");
        }
    
        /**
         * 上传
         *
         * @param minioClient
         */
        public static void upload(MinioClient minioClient, String bucketName, String source, String targetname) {
            try {
                // 如果不存在“asiatrip”存储桶,则生成“asiatrip”。
                boolean found =
                        minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
                if (!found) {
                    // 创建一个 'asiatrip'存储桶
                    minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                } else {
                    System.out.println("Bucket 'asiatrip' already exists.");
                }
    
                // 将“c盘文件b.jpg”作为对象名“d.jpg”上传到'asiatrip'桶里
                minioClient.uploadObject(
                        UploadObjectArgs.builder()
                                .bucket(bucketName)
                                .object(targetname)
                                .filename(source)
                                .build());
            } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) {
                System.out.println("Error occurred: " + e);
            }
        }
    
        /**
         * 删除
         *
         * @param fileName
         * @return
         * @throws Exception
         */
        public static void remove(MinioClient minioClient, String bucketName, String fileName) {
            try {
                minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 下载
         *
         * @return
         */
        public static void downloadMinio(MinioClient minioClient, String savepath, String bucketName, String fileName) {
            try {
                InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
                FileOutputStream fileOutputStream = new FileOutputStream(savepath);
                byte[] buf = new byte[1024];
                int bytesRead;
                while ((bytesRead = stream.read(buf, 0, buf.length)) >= 0) {
                    fileOutputStream.write(buf, 0, bytesRead);
                    fileOutputStream.flush();
                }
                fileOutputStream.close();
                stream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }
    
    • 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

    封装的Util工具类:

    package com.example.demo.minio;
    
    import io.minio.*;
    import io.minio.errors.*;
    import io.minio.messages.Bucket;
    import io.minio.messages.Item;
    
    import java.io.*;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.text.DecimalFormat;
    import java.util.*;
    
    /**
     * @author :Mall
     * @date :Created in 2021-11-26
     * @description :MinIO工具类
     * 

    * 为保证代码维护性,如需修改或使用遇到问题,请与作者联系。 * 支持MinIO 8.3.X *

    */
    public class JavaMinIOUtil { //MinIO的API请求地址 private final static String ENDPOINT = "http://192.168.1.86:90"; //用户名 private final static String USERNAME = "admin"; //密码 private final static String PASSWORD = "12345678"; //单例加载 private final static MinioClient minioClient = MinioClient.builder().endpoint(ENDPOINT).credentials(USERNAME, PASSWORD).build(); /** * 创建桶,如果不存在则创建,存在跳过 * * @param bucketName */ public static void createBucket(String bucketName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException { boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); if (!found) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); } } /** * 获取桶列表 * * @returnF */ public static List<Bucket> findBucketList() { try { return minioClient.listBuckets(); } catch (InvalidKeyException e) { e.printStackTrace(); return null; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } catch (ErrorResponseException e) { e.printStackTrace(); return null; } catch (InvalidResponseException e) { e.printStackTrace(); return null; } catch (ServerException e) { e.printStackTrace(); return null; } catch (InsufficientDataException e) { e.printStackTrace(); return null; } catch (XmlParserException e) { e.printStackTrace(); return null; } catch (InternalException e) { e.printStackTrace(); return null; } catch (IOException ioException) { ioException.printStackTrace(); return null; } } /** * 根据桶名称,获取对象列表 * * @param bucketName 桶名称 * @param path 桶路径 .如 aaa文件夹则传入 aaa/ * @return */ public static List<Item> findObjectList(String bucketName, String path) { try { createBucket(bucketName); Iterable<Result<Item>> myObjects = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(path).build()); Iterator<Result<Item>> iterator = myObjects.iterator(); List<Item> list = new ArrayList<>(); while (iterator.hasNext()) { list.add(iterator.next().get()); } return list; } catch (InvalidKeyException e) { e.printStackTrace(); return null; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } catch (ErrorResponseException e) { e.printStackTrace(); return null; } catch (InvalidResponseException e) { e.printStackTrace(); return null; } catch (ServerException e) { e.printStackTrace(); return null; } catch (InsufficientDataException e) { e.printStackTrace(); return null; } catch (XmlParserException e) { e.printStackTrace(); return null; } catch (InternalException e) { e.printStackTrace(); return null; } catch (IOException ioException) { ioException.printStackTrace(); return null; } } /** * 文件状态,可查看是否存在 * * @param bucketName 桶名称 * @param filePath 上传的资源路径,保护名称。如果是根目录,就是文件名称 * @return */ public static StatObjectResponse getObjectState(String bucketName, String filePath) { try { createBucket(bucketName); return minioClient.statObject( StatObjectArgs.builder().bucket(bucketName).object(filePath).build()); } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } /** * 上传文件 * * @param bucketName 桶名称 * @param file 上传资源 * @param filePath 上传的资源路径,保护名称。如果是根目录,就是文件名称 * @return */ public static boolean upload(String bucketName, InputStream file, String filePath) { try { createBucket(bucketName); minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(filePath).stream(file, file.available(), -1).build()); return true; } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) { e.printStackTrace(); return false; } } /** * 下载文件 * * @param bucketName 桶名称 * @param filePath 上传的资源路径,保护名称。如果是根目录,就是文件名称 * @return */ public static InputStream download(String bucketName, String filePath) { try { createBucket(bucketName); return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(filePath).build()); } catch (InvalidKeyException e) { e.printStackTrace(); return null; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } catch (ErrorResponseException e) { e.printStackTrace(); return null; } catch (InvalidResponseException e) { e.printStackTrace(); return null; } catch (ServerException e) { e.printStackTrace(); return null; } catch (InsufficientDataException e) { e.printStackTrace(); return null; } catch (XmlParserException e) { e.printStackTrace(); return null; } catch (InternalException e) { e.printStackTrace(); return null; } catch (IOException ioException) { ioException.printStackTrace(); return null; } } /** * 删除文件 * * @param bucketName 桶名称 * @param filePath 资源名称 * @return */ public static boolean remove(String bucketName, String filePath) { try { minioClient.removeObject( RemoveObjectArgs.builder().bucket(bucketName).object(filePath).build()); return true; } catch (InvalidKeyException e) { e.printStackTrace(); return false; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return false; } catch (ErrorResponseException e) { e.printStackTrace(); return false; } catch (InvalidResponseException e) { e.printStackTrace(); return false; } catch (ServerException e) { e.printStackTrace(); return false; } catch (InsufficientDataException e) { e.printStackTrace(); return false; } catch (XmlParserException e) { e.printStackTrace(); return false; } catch (InternalException e) { e.printStackTrace(); return false; } catch (IOException ioException) { ioException.printStackTrace(); return false; } } /** * 文件大小转换 * * @param fileSize 文件大小,单位b * @return */ private static String formatFileSize(long fileSize) { DecimalFormat df = new DecimalFormat("#.00"); String fileSizeString = ""; String wrongSize = "0B"; if (fileSize == 0) { return wrongSize; } if (fileSize < 1024) { fileSizeString = df.format((double) fileSize) + " B"; } else if (fileSize < 1048576) { fileSizeString = df.format((double) fileSize / 1024) + " KB"; } else if (fileSize < 1073741824) { fileSizeString = df.format((double) fileSize / 1048576) + " MB"; } else { fileSizeString = df.format((double) fileSize / 1073741824) + " GB"; } return fileSizeString; } public static void main(String[] args) throws Exception { FileInputStream fileInputStream = new FileInputStream(new File("G:/a.jpg")); //上传 //upload("bucket02", fileInputStream, "ccc.jpg"); //下载 //InputStream inputStream = download("bucket02", "ccc.jpg"); //IOUtils.copy(inputStream, new FileOutputStream(new File("G:/ddd.jpg"))); //删除 //remove("bucket02", "ccc.jpg"); //查看文件状态 //StatObjectResponse statObject = getObjectState("bucket02", "b.jpg"); //获取所有桶 //findBucketList().stream().forEach(item -> System.out.println(item.name())); //获取桶根目录所有对象 //findObjectList("bucket02", "aaa/").stream().forEach(item -> System.out.println(item.objectName())); } }
    • 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
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295

    SpringBoot集成

    第一步

    引入pom.xml依赖。

    <!-- MinIO 依赖 -->
    <dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>8.3.3</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    第二步

    配置application.yml文件

    minio:
      endpoint: http://192.168.1.86:90
      username: admin
      password: 12345678
      bucketname: bucket02
    
    • 1
    • 2
    • 3
    • 4
    • 5

    第三步

    增加MinIOConfig配置类。

    package com.example.demo.minio;
    
    import io.minio.MinioClient;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author :Mall
     * @date :Created in 2021-11-26
     * @description :MinIO配置类
     */
    @Configuration
    public class MinIOConfig {
        //MinIO的API地址
        @Value("${minio.endpoint}")
        private String endpoint;
        @Value("${minio.username}")
        private String username;
        @Value("${minio.password}")
        private String password;
    
        @Bean
        public MinioClient minioClient() {
            return MinioClient.builder().endpoint(endpoint).credentials(username, password).build();
        }
    }
    
    • 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

    第四步

    增加MinIOUtil工具类

    package com.example.demo.minio;
    
    import io.minio.*;
    import io.minio.errors.*;
    import io.minio.messages.Bucket;
    import io.minio.messages.Item;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    /**
     * @author :Mall
     * @date :Created in 2021-11-26
     * @description :MinIO工具类
     * 

    * 为保证代码维护性,如需修改或使用遇到问题,请与作者联系。 * 支持MinIO 8.3.X *

    */
    @Component public class MinIOUtil { @Autowired private MinioClient minioClient; @Value("${minio.bucketname}") private String bucketName; /** * 创建桶,如果不存在则创建,存在跳过 */ public void createBucket() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException { boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); if (!found) { minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); } } /** * 获取桶列表 * * @returnF */ public List<Bucket> findBucketList() { try { return minioClient.listBuckets(); } catch (InvalidKeyException e) { e.printStackTrace(); return null; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } catch (ErrorResponseException e) { e.printStackTrace(); return null; } catch (InvalidResponseException e) { e.printStackTrace(); return null; } catch (ServerException e) { e.printStackTrace(); return null; } catch (InsufficientDataException e) { e.printStackTrace(); return null; } catch (XmlParserException e) { e.printStackTrace(); return null; } catch (InternalException e) { e.printStackTrace(); return null; } catch (IOException ioException) { ioException.printStackTrace(); return null; } } /** * 根据桶名称,获取对象列表 * * @param path 桶路径 * @return */ public List<Item> findObjectList(String path) { try { createBucket(); Iterable<Result<Item>> myObjects = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build()); Iterator<Result<Item>> iterator = myObjects.iterator(); List<Item> list = new ArrayList<>(); while (iterator.hasNext()) { list.add(iterator.next().get()); } return list; } catch (InvalidKeyException e) { e.printStackTrace(); return null; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } catch (ErrorResponseException e) { e.printStackTrace(); return null; } catch (InvalidResponseException e) { e.printStackTrace(); return null; } catch (ServerException e) { e.printStackTrace(); return null; } catch (InsufficientDataException e) { e.printStackTrace(); return null; } catch (XmlParserException e) { e.printStackTrace(); return null; } catch (InternalException e) { e.printStackTrace(); return null; } catch (IOException ioException) { ioException.printStackTrace(); return null; } } /** * 文件状态,可查看是否存在 * * @param filePath 上传的资源路径,保护名称。如果是根目录,就是文件名称 * @return */ public StatObjectResponse getObjectState(String filePath) { try { createBucket(); return minioClient.statObject( StatObjectArgs.builder().bucket(bucketName).object(filePath).build()); } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } /** * 上传文件 * * @param file 上传资源 * @param filePath 上传的资源路径,保护名称。如果是根目录,就是文件名称 * @return */ public boolean upload(InputStream file, String filePath) { try { createBucket(); minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(filePath).stream(file, file.available(), -1).build()); return true; } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) { e.printStackTrace(); return false; } } /** * 下载文件 * * @param filePath 上传的资源路径,保护名称。如果是根目录,就是文件名称 * @return */ public InputStream download(String filePath) { try { createBucket(); return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(filePath).build()); } catch (InvalidKeyException e) { e.printStackTrace(); return null; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } catch (ErrorResponseException e) { e.printStackTrace(); return null; } catch (InvalidResponseException e) { e.printStackTrace(); return null; } catch (ServerException e) { e.printStackTrace(); return null; } catch (InsufficientDataException e) { e.printStackTrace(); return null; } catch (XmlParserException e) { e.printStackTrace(); return null; } catch (InternalException e) { e.printStackTrace(); return null; } catch (IOException ioException) { ioException.printStackTrace(); return null; } } /** * 删除文件 * * @param filePath 资源名称 * @return */ public boolean remove(String filePath) { try { minioClient.removeObject( RemoveObjectArgs.builder().bucket(bucketName).object(filePath).build()); return true; } catch (InvalidKeyException e) { e.printStackTrace(); return false; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return false; } catch (ErrorResponseException e) { e.printStackTrace(); return false; } catch (InvalidResponseException e) { e.printStackTrace(); return false; } catch (ServerException e) { e.printStackTrace(); return false; } catch (InsufficientDataException e) { e.printStackTrace(); return false; } catch (XmlParserException e) { e.printStackTrace(); return false; } catch (InternalException e) { e.printStackTrace(); return false; } catch (IOException ioException) { ioException.printStackTrace(); return false; } } /** * 文件大小转换 * * @param fileSize 文件大小,单位b * @return */ public String formatFileSize(long fileSize) { DecimalFormat df = new DecimalFormat("#.00"); String fileSizeString = ""; String wrongSize = "0B"; if (fileSize == 0) { return wrongSize; } if (fileSize < 1024) { fileSizeString = df.format((double) fileSize) + " B"; } else if (fileSize < 1048576) { fileSizeString = df.format((double) fileSize / 1024) + " KB"; } else if (fileSize < 1073741824) { fileSizeString = df.format((double) fileSize / 1048576) + " MB"; } else { fileSizeString = df.format((double) fileSize / 1073741824) + " GB"; } return fileSizeString; } }
    • 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
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273

    第五步

    增加测试Controller实现功能。

    package com.example.demo.controller;
    
    import com.example.demo.minio.MinIOUtil;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    import io.minio.StatObjectResponse;
    import io.minio.messages.Bucket;
    import org.apache.commons.io.IOUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author :Mall
     * @date :Created in 2021-11-26
     * @description :
     */
    @RestController()
    @RequestMapping("/minio")
    public class MinIOController {
        @Autowired
        private MinIOUtil minIOUtil;
    
        /**
         * Bucket列表
         *
         * @return
         */
        @RequestMapping("/bucketlist")
        public List<Map<String, String>> bucketlist() {
            List<Map<String, String>> list = new ArrayList<>();
            minIOUtil.findBucketList().stream().forEach(item -> {
                Map<String, String> map = new HashMap<>();
                map.put("name", item.name());
                map.put("date", item.creationDate().toString());
                list.add(map);
            });
            return list;
        }
    
        /**
         * 根据路径获取对象列表
         *
         * @param path
         * @return
         */
        @RequestMapping("/findObjectList")
        public List<Map<String, String>> findObjectList(String path) {
            List<Map<String, String>> list = new ArrayList<>();
            minIOUtil.findObjectList(path).stream().forEach(item -> {
                Map<String, String> map = new HashMap<>();
                map.put("name", item.objectName());
                map.put("size", minIOUtil.formatFileSize(item.size()));
                list.add(map);
            });
            return list;
        }
    
        /**
         * 上传文件
         */
    
        @RequestMapping("/upload")
        public void upload(@RequestParam(name = "file", required = false) MultipartFile[] file) throws IOException {
            for (MultipartFile multipartFile : file) {
                minIOUtil.upload(multipartFile.getInputStream(), multipartFile.getOriginalFilename());
            }
        }
    
        /**
         * 下载文件
         */
    
        @RequestMapping("/download")
        public void download(HttpServletResponse response, String filename) throws IOException {
            InputStream download = minIOUtil.download(filename);
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
            IOUtils.copy(download, response.getOutputStream());
        }
    
        /**
         * 删除文件
         */
    
        @RequestMapping("/remove")
        public void remove(HttpServletResponse response, String filepath) throws IOException {
            minIOUtil.remove(filepath);
        }
    
    }
    
    • 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
  • 相关阅读:
    gpt扣款失败,openai扣款失败无法使用-如何解决gpt扣款失败的问题?
    JavaScript系列之数字类型
    (五)CSS前端开发面试会问到的问题有哪些?
    观点|周鸿祎:大模型真正的竞争在于使其与用户场景相结合
    【论文 01】《Attention is all you need》
    红黑树以及JAVA实现(一)
    关于游戏公司组织架构的小讨论
    PHREEQC建模及典型案例解析与高阶拓展应用【反向“编译”、“玩转”后处理技术、GibbsStudio和PhreePlo方法】
    【c语言基础题】— —第四版,可当作日常练习和期末复习,有奇效哟!
    JavaScript-----元素可视区client
  • 原文地址:https://blog.csdn.net/u014386444/article/details/126035944