• SpringBoot整合MinIO


    在这里插入图片描述

    1 问题背景

    前面搭建了MinIO容器,现在来研究SpringBoot整合MinIO

    2 前言

    MinIO的官方文档给出了Java如何使用MinIO的教程,详情见官方文档,笔者根据该官方文档简单地实战。并针对某些注意事项做特别详细介绍。

    3 步骤

    3.1 引入依赖

    引入MinIO的依赖

    
        io.minio
        minio
        8.4.6
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    MinIO的客户端需要用到OKHttp,因此把OKHttp的依赖也引入进来,不引入会在创建minio客户端的过程中会报错

    
        com.squareup.okhttp3
        okhttp
        4.9.0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.2 连接MinIO服务端的必要参数

    参数名解释
    EndpointMinIO服务端所在的服务器地址,格式为:http协议://MInIO服务端所在的ip地址:MinIO的API端口号
    Access Key访问MinIO的key,在MinIO控制台获取
    Secret Key访问MinIO的密钥,在MinIO控制台获取

    3.3 代码实现文件上传功能

    public class FileUploader {
    
        // 桶名
        private final static String BUCKET_NAME =  "asia";
        // 待上传的文件所在的路径,此处我用windows系统,linux系统一样是写路径
        private final static String FILE_NAME = "D:\\Google Download\\博客图片\\yunying.png";
        // 文件上传后的名字
        private final static String OBJECT_NAME = "yunying.png";
    
    
        public static void main(String[] args)
                throws IOException, NoSuchAlgorithmException, InvalidKeyException {
            try {
    
                MinIOProperties minIOProperties = getMinIOProperties();
    
    
                // Create a minioClient with the MinIO server playground, its access key and secret key.
                MinioClient minioClient =
                        MinioClient.builder()
                                .endpoint(minIOProperties.getEndpoint())
                                .credentials(minIOProperties.getAccessKey(), minIOProperties.getSecretKey())
                                .build();
    
                // Make 'asiatrip' bucket if not exist.
                boolean found =
                        minioClient.bucketExists(BucketExistsArgs.builder().bucket(BUCKET_NAME).build());
                if (!found) {
                    // Make a new bucket called 'asiatrip'.
                    minioClient.makeBucket(MakeBucketArgs.builder().bucket(BUCKET_NAME).build());
                } else {
                    System.out.println("Bucket '" + BUCKET_NAME + "' already exists.");
                }
    
                // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
                // 'asiatrip'.
                minioClient.uploadObject(
                        UploadObjectArgs.builder()
                                .bucket(BUCKET_NAME)
    //                            .object("asiaphotos-2015.zip")
                                .object(OBJECT_NAME)
    //                            .filename("/home/user/Photos/asiaphotos.zip")
                                .filename(FILE_NAME)
                                .build());
                System.out.println(
                        "'" + FILE_NAME + "' is successfully uploaded as "
                                + "object '" + OBJECT_NAME + "' to bucket '" + BUCKET_NAME + "'.");
            } catch (MinioException e) {
                System.out.println("Error occurred: " + e);
                System.out.println("HTTP trace: " + e.httpTrace());
            }
        }
    
        /**
         * 构造MinIO Server的配置
         * @return
         */
        private static MinIOProperties getMinIOProperties(){
            MinIOProperties properties = new MinIOProperties();
            properties.setEndpoint("http://192.168.163.128:9001");
            properties.setAccessKey("kLCCny4gsGsNYrMN");
            properties.setSecretKey("2eXuvFDbf28Uk8FAf2LMXTj0rRTU43tt");
            return properties;
        }
    }
    
    • 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

    3.4 验证

    前往MinIO控制台查看是否成功上传了图片。图片的访问路径是MInIO服务端所在的ip地址:MinIO的API端口号/桶名/文件名

  • 相关阅读:
    经典文献阅读之--BoW3D
    SaaSBase:什么是易客管家SCRM?
    如何拼接两张图片?
    二、react的组件-state-props-setState
    hadoop3.3.1单机版环境搭建详细流程记录
    python 拆分pdf(有可执行文件exe)
    第一章:网络协议的奥秘
    Linux上执行内存中的脚本和程序
    猿创征文 |【数据结构】3个例题带你搞定图的遍历:深度优先搜索
    使用宝塔面板部署商城项目到云服务器的案例
  • 原文地址:https://blog.csdn.net/qq_40634846/article/details/128165809