SpringBoot整合Minio

pom.xml 导入 minio依赖,如下:

<dependency>
    <groupId>io.miniogroupId>
    <artifactId>minioartifactId>
    <version>8.0.3version>
dependency>

application.yml 配置minio连接信息

minio:
  endpoint: http://192.168.1.11:9000
  username: admin
  password: admin
  bucketName: test

Minio 配置类

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Minio 配置
 *
 * @author terry
 * @version 1.0
 * @date 2022/8/21 21:01
 */
@Configuration
@EnableConfigurationProperties(MinioConfig.MinioProperties.class)
public class MinioConfig {

    @ConfigurationProperties(prefix = "minio")
    @Data
    public static class MinioProperties{

        private String endpoint;

        private String userName;

        private String password;

        private String bucketName;
    }

    @Bean
    public MinioClient minioClient(MinioProperties minio) {
        return MinioClient.builder()
            .endpoint(minio.getEndpoint())
            .credentials(minio.getUserName(), minio.getPassword())
            .build();
    }
}

Minio操作类

import com.terry.config.MinioConfig;
import io.minio.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.UUID;

/**
 * minio 操作类
 *
 * @author terry
 * @version 1.0
 * @date 2022/8/22 21:18
 */
@Service
@Slf4j
public class MinioService {
    @Autowired
    private MinioClient client;

    @Autowired
    private MinioConfig.MinioProperties minioProperties;

    public void upload(InputStream inputStream, String name) {
        String bucketName = minioProperties.getBucketName();
        String endpoint = minioProperties.getEndpoint();
        try {
            // 判断存储桶是否存在  不存在则创建
            createBucket(bucketName);
            // 新的文件名
            String fileName = UUID.randomUUID().toString() + name.substring(name.lastIndexOf("."));
            // 开始上传
            client.putObject(PutObjectArgs.builder().bucket(bucketName)
                    .object(fileName).stream(inputStream, inputStream.available(), -1).build());
            String url = bucketName + "/" + fileName;
            log.info(" 相对路径: " + url);
            log.info(" 完整路径: " + endpoint  + "/" + url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void createBucket(String bucketName) {
        try {
            if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
                client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                StringBuilder builder = new StringBuilder();
                builder.append("{\n");
                builder.append("    \"Statement\": [\n");
                builder.append("        {\n");
                builder.append("            \"Action\": [\n");
                builder.append("                \"s3:GetBucketLocation\",\n");
                builder.append("                \"s3:ListBucket\"\n");
                builder.append("            ],\n");
                builder.append("            \"Effect\": \"Allow\",\n");
                builder.append("            \"Principal\": \"*\",\n");
                builder.append("            \"Resource\": \"arn:aws:s3:::%s\"\n");
                builder.append("        },\n");
                builder.append("        {\n");
                builder.append("            \"Action\": \"s3:GetObject\",\n");
                builder.append("            \"Effect\": \"Allow\",\n");
                builder.append("            \"Principal\": \"*\",\n");
                builder.append("            \"Resource\": \"arn:aws:s3:::%s/*\"\n");
                builder.append("        }\n");
                builder.append("    ],\n");
                builder.append("    \"Version\": \"2012-10-17\"\n");
                builder.append("}\n");
                String config = String.format(builder.toString(), bucketName, bucketName);
                try {
                    client.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(config).build());
                } catch (Exception e) {
                    log.info("[minio] 设置桶:{}策略失败", bucketName, e);
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

测试 Minio

import com.terry.service.MinioService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * 测试 Minio
 *
 * @author terry
 * @version 1.0
 * @date 2022/8/16 22:19
 */
@SpringBootTest
public class MinioTest {

    @Autowired
    private MinioService minioService;

    @Test
    public void test() throws FileNotFoundException {
        minioService.upload(new FileInputStream("D:\\test.txt"), "test.txt");
    }
}

你可能感兴趣的:(SpringBoot,spring,boot,java,spring,minio)