第五节 SpringBoot项目操作MinIO文件

亮子 2023-03-28 14:37:09 17121 0 0 0

1、引入依赖如下:

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

2、测试代码

package com.shenma2005.order;

import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

public class ServerMinioTests {

    @Test
    void testMinio() {
        try {
            //--1 初始化变量
            String bucketName = "public";
            String uploadFile = "D:\\test\\shenma-cms-logo.png";
            String objectName = "abc.png";
            String downloadFile = "D:\\test\\shenma-cms-logo3.png";

            //--2 创建MinIO对象
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://8.142.83.78:9000")
                            .credentials("david", "Ab.123456789")
                            .build();

            //--3 检查Bucket是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!found) {
                // 如果没有bucket则新创建一个
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            } else {
                System.out.println("Bucket 'public' already exists.");
            }

            //--4 上传文件
            UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .filename(uploadFile)
                    .build();
            ObjectWriteResponse response = minioClient.uploadObject(uploadObjectArgs);
            System.out.println("file is uploaded.");

            //--4 判断文件是否存在
            DownloadObjectArgs downloadObjectArgs = DownloadObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .filename(downloadFile)
                    .build();
            minioClient.downloadObject(downloadObjectArgs);

            //--5 生成外链
            GetPresignedObjectUrlArgs objectUrlArgs = GetPresignedObjectUrlArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .expiry(10, TimeUnit.MINUTES)
                    .method(Method.GET)
                    .build();
            String objectUrl = minioClient.getPresignedObjectUrl(objectUrlArgs);
            System.out.println("objectUrl="+objectUrl);

            //--6 删除文件对象
            RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .build();
            minioClient.removeObject(removeObjectArgs);

        } catch (MinioException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }

}