SpringBoot整合MinIO

在这里插入图片描述

1 问题背景

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

2 前言

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

3 步骤

3.1 引入依赖

引入MinIO的依赖

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.4.6</version>
</dependency>

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

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.9.0</version>
</dependency>

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;
    }
}

3.4 验证

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值