• springboot集成minio,docker部署


    docker部署minio

    docker run -p 9000:9000 -p 9090:9090 --name minio     -v ~/minio/data:/data     -e MINIO_ROOT_USER=root     -e MINIO_ROOT_PASSWORD=chaiyinlei     -d minio/minio server /data --console-address ":9090"
    
    • 1

    springboot配置

        <dependency>
          <groupId>io.minio</groupId>
          <artifactId>minio</artifactId>
          <version>8.5.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
        <dependency>
          <groupId>com.squareup.okhttp3</groupId>
          <artifactId>okhttp</artifactId>
          <version>4.11.0</version>
        </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    application.yml

    minIo:
      endpoint: http://你的ip地址:9000
      accessKey: root
      secretKey: chaiyinlei
    
    • 1
    • 2
    • 3
    • 4

    minioconfig

    @Data
    @Configuration
    public class MinIoConfig {
    
        @Value(value = "${minIo.endpoint}")
        private String endpoint;
        @Value(value = "${minIo.accessKey}")
        private String accessKey;
        @Value(value = "${minIo.secretKey}")
        private String secretKey;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    minioutils

    package com.test.empback.utils;
    
    import com.test.empback.config.MinIoConfig;
    import io.minio.*;
    import io.minio.errors.*;
    import io.minio.http.Method;
    import io.minio.messages.Bucket;
    import io.minio.messages.Item;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.charset.StandardCharsets;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.util.ArrayList;
    import java.util.List;
    
    
    @Slf4j
    @Component
    @Configuration
    public class MinIoUtil {
    
    
        private MinIoConfig minIO;
    
        private static MinioClient minioClient;
    
        public MinIoUtil(MinIoConfig minIO) {
            this.minIO = minIO;
        }
    
    
        /**
         * @param
         * @Author: yinlei
         * @Description: 初始化
         * @Date:
         * @Return void
         */
        @PostConstruct //@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次
        public void init() { //构建minio客户端
            minioClient = MinioClient.builder()
                    .endpoint(minIO.getEndpoint())
                    .credentials(minIO.getAccessKey(), minIO.getSecretKey())
                    .build();
        }
    
    
    
        /**
         * @param bucketName 桶名
         * @Author: yinlei
         * @Description: 判断bucket是否存在
         * @Date: 2023/09/23 15:19
         * @Return boolean
         */
        public boolean bucketExists(String bucketName) {
            try {
                return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return false;
        }
    
    
        /**
         * @param bucketName 桶名
         * @Author: yinlei
         * @Description: 创建桶
         * @Date: 2023/09/23 15:52
         * @Return void
         */
        public void makeBucket(String bucketName) {
            try {
                boolean isExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
                if (!isExists) {
                    minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    
        /**
         * @param
         * @Author: yinlei
         * @Description: 列出所有存储桶的存储信息
         * @Date:
         * @Return void
         */
        public void listBuckets() {
            List<Bucket> bucketList = null;
            try {
                bucketList = minioClient.listBuckets();
                for (Bucket bucket : bucketList) {
                    System.out.println(bucket.creationDate() + ", " + bucket.name());
    
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    
    
        /**
         * @param bucketName 桶名
         * @Author: yinlei
         * @Description: 列出存储桶的对象信息
         * @Date:
         * @Return void
         */
        public List<String> listObject(String bucketName) {
            List<String> fileList = new ArrayList<>();
            try {
                Iterable<Result<Item>> results = minioClient.listObjects(
                        ListObjectsArgs.builder().bucket(bucketName).build());
                for (Result<Item> result : results) {
                    Item item = null;
                    item = result.get();
                    fileList.add(item.objectName());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return fileList;
        }
    
    //    public Iterable<Result<Item>> listObject(String bucketName, String prefix) throws XmlParserException {
    //        return minioClient.listObjects(bucketName, prefix);
    //    }
        /**
         * @param bucketName 桶名
         * @Author: yinlei
         * @Description: 删除桶
         * @Date: 
         * @Return void
         */
        public void removeBucket(String bucketName) {
            try {
                minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * @param bucketName 桶名
         * @param objectName 存储桶里的对象名称
         * @param fileName   文件名
         * @Author: yinlei
         * @Description: 文件上传
         * @Date: 
         * @Return void
         */
        public void uploadObject(String bucketName, String objectName, String fileName) {
    
            try {
                minioClient.uploadObject(
                        UploadObjectArgs.builder()
                                .bucket(bucketName)
                                .object(objectName)
                                .filename(fileName)
    
                                .build());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * @param bucketName  桶名
         * @param objectName  存储桶里的对象名称
         * @param stream      要上传的流
         * @param contentType 文件类型
         * @Author: yinlei
         * @Description: 上传文件(流式)
         * @Date: 2023/09/24 10:12
         * @Return void
         */
        public void putObject(String bucketName, String objectName, InputStream stream, String contentType) {
            try {
                minioClient.putObject(PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .stream(
                                stream, -1, 10485760
                        )
                        .contentType(contentType)
                        .build());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * @param bucketName 桶名
         * @param objectName 存储桶里的对象名称
         * @Author: yinlei
         * @Description: 删除文件
         * @Date: 2023/09/24 10:14
         * @Return void
         */
        public void removeObject(String bucketName, String objectName) {
            try {
                minioClient.removeObject(
                        RemoveObjectArgs.builder()
                                .bucket(bucketName)
                                .object(objectName)
                                .build());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * @param bucketName 桶名
         * @param objectName 存储桶里的对象名称
         * @param saveDir   把文件下载到这里
         * @Author: yinlei
         * @Description: 下载文件
         * @Date: 2023/09/24 10:17
         * @Return void
         */
        public void download(String bucketName, String objectName, String saveDir) {
            try {
                minioClient.downloadObject(
                        DownloadObjectArgs.builder()
                                .bucket(bucketName)
                                .object(objectName)
                                .filename(saveDir)
                                .build());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void downloadFile(String bucketName, String fileName, HttpServletResponse response) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {
            InputStream fileStream = minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .build()
            );
            String[] strings = fileName.split("/");
            fileName = strings[strings.length - 1];
            System.out.println("filename=" + fileName);
            String fileNameCode = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
    //        response.setContentType( "application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileNameCode);
            ServletOutputStream outputStream = response.getOutputStream();
            // 输出文件
            int length;
            byte[] buffer = new byte[1024];
            while ((length = fileStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
            outputStream.flush();
            fileStream.close();
            outputStream.close();
        }
    
    
    
    
        /**
         * @param bucketName 桶名
         * @param objectName 存储桶里的对象名称
         * @Author: yinlei
         * @Description: 获取文件外链
         * @Date: 2023/09/24 10:18
         * @Return String
         */
        public String getObjectUrl(String bucketName, String objectName) {
            try {
    //            String url = minioClient.getObjectUrl(bucketName, objectName);
                String url = minioClient.getPresignedObjectUrl(
                        GetPresignedObjectUrlArgs
                                .builder()
                                .method(Method.GET)
                                .bucket(bucketName)
                                .object(objectName)
                                .build());
                System.out.println(bucketName + "——" + objectName + " can be downloaded by: " + url);
                return url;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
    
        public boolean isFileExisted(String fileName, String bucketName) {
            boolean flag = false;
            InputStream inputStream = null;
            try {
                inputStream = minioClient.getObject(
                        GetObjectArgs.builder()
                                .bucket(bucketName)
                                .object(fileName)
                                .build());
                if (inputStream != null) {
                    flag = true;
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                flag = false;
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        log.error(e.getMessage());
                    }
                }
            }
            return flag;
        }
    
        /**
         * 获取文件流
         */
        public InputStream getInputStream(String bucketName, String fileName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
            InputStream inputStream = null;
            inputStream = minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .build());
            return inputStream;
        }
    }
    
    
    • 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
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340

    ok,搞定。

  • 相关阅读:
    淘宝/天猫获取购买到的商品订单物流信息 API分享
    vue+Fullcalendar
    调用matlab曲线拟合工具箱,自定义函数预测人口数量
    基于C语言的词法分析实验
    一文搞懂│php 中的 DI 依赖注入
    ASEMI整流桥GBU610参数,GBU610规格
    C++:类与面向对象&static和this关键字&其他关键字
    html 菜单点击切换样式,菜单<a> 控制iframe
    环保行业B2B撮合管理系统实现产业优化升级,提升企业业务能力
    成都营运《乡村振兴战略下传统村落文化旅游设计》许少辉八一著作
  • 原文地址:https://blog.csdn.net/qq_37699336/article/details/133225272