spring boot 集成minio

spring boot 集成minio

minio服务的启动我就不详细说了,有需要的小伙伴可以参见我的另外一篇文章

pom依赖

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

加入minio配置

minio:
  enable: true  # 必须配置 true
  enableHttps: false # 是否使用https
  url: http://0.0.0.0:19300 # minio 地址
  accessKey: admin  # Minio 账户名
  secretKey: fhfh123 # minio 密码
  bucketName: aaaaaa # 文件存储块

编写配置读取工具类

@Data
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
    private Boolean enableHttps = false;
    /**
     * 服务地址
     */
    private String url;
    /**
     * 用户名
     */
    private String accessKey;
    /**
     * 密码
     */
    private String secretKey;
    /**
     * 存储桶名称
     */
    private String bucketName;
}

编写初始化MinioClient

@Configuration
@ConditionalOnExpression("${minio.enable:false}")
public class InitClient {

    @Autowired
    MinioConfig minioConfig;

    @Bean
    public MinioClient minioClient(){

        MinioClient.Builder builder = MinioClient.builder();
        if (minioConfig.getEnableHttps()){
            builder.httpClient(OKHttpClientBuilder.buildOKHttpClient().build());
        }
        return builder.endpoint(minioConfig.getUrl()).credentials(minioConfig.getAccessKey()
                ,minioConfig.getSecretKey()).build();

    }

}

编写文件处理工具方法

@Service
@ConditionalOnExpression("${minio.enable:false}")
public class MinioFileServiceImpl  implements IFileService {

    @Autowired
    MinioClient minioClient;

    @Autowired
    MinioConfig minioConfig;

    @Override
    public String uploadFile(MultipartFile file) throws Exception {

        String fileName = FileUploadUtils.extractFilename(file);
        // 检测是否存在该桶
        if (!minioClient.bucketExists(
                BucketExistsArgs.builder().bucket(minioConfig.getBucketName()).build()
        )){
            //不存在就新建
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioConfig.getBucketName()).build());
        }

        PutObjectArgs args = PutObjectArgs.builder()
                .bucket(minioConfig.getBucketName())
                .object(fileName)
                .stream(file.getInputStream(), file.getSize(), -1)
                .contentType(file.getContentType())
                .build();
        minioClient.putObject(args);



        return  fileName;
    }

    @Override
    public String getDownloadUrl(String fileName) throws Exception  {
        //返回下载url
        return minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .bucket(minioConfig.getBucketName())
                        .object(fileName)
                        .method(Method.GET)
                        .build());
    }
}

minio client 支持https

再初始化客户端时加入ssl 配置

 builder.httpClient(OKHttpClientBuilder.buildOKHttpClient().build());

OKHttpClientBuilder工具类


public class OKHttpClientBuilder {
    public static OkHttpClient.Builder buildOKHttpClient() {
        try {
            TrustManager[] trustAllCerts = buildTrustManagers();
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
            builder.hostnameVerifier((hostname, session) -> true);
            return builder;
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
            return new OkHttpClient.Builder();
        }
    }

    private static TrustManager[] buildTrustManagers() {
        return new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[]{};
                    }
                }
        };
    }
}

你可能感兴趣的:(JAVA,spring,cloud,软件工具组件,java,spring)