码迷,mamicode.com
首页 > 编程语言 > 详细

Java MinIO 集成

时间:2021-06-02 17:42:43      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:accesskey   package   beans   分隔符   bool   ota   ica   ice   cli   

完整的项目在 GitHub-mlb

配置类

package com.seliote.mlb.fs.config;

import com.seliote.mlb.common.config.PropertiesInjector;
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * MinIO 配置类
 *
 * @author seliote
 */
@Slf4j
@Configuration
public class MinioConfig {

    private final PropertiesInjector.Minio minioProp;

    @Autowired
    public MinioConfig(PropertiesInjector propertiesInjector) {
        this.minioProp = propertiesInjector.getMinio();
    }

    /**
     * MinioClient Bean
     *
     * @return MinioClient 对象
     */
    @Bean
    public MinioClient minioClient()
            throws MinioException, IOException, NoSuchAlgorithmException, InvalidKeyException {
        var client = MinioClient.builder()
                .endpoint(minioProp.getEndpoint())
                .credentials(minioProp.getAccessKey(), minioProp.getSecretKey())
                .build();
        // 检查 Bucket 是否存在
        boolean found = client.bucketExists(BucketExistsArgs.builder()
                .bucket(minioProp.getBucket())
                .build());
        if (!found) {
            log.error("Bucket {} not found! Please create it first", minioProp.getBucket());
            throw new IllegalStateException("Bucket not found");
        }
        log.info("Minio bucket: {}", minioProp.getBucket());
        return client;
    }
}

Service

package com.seliote.mlb.fs.service;

import com.seliote.mlb.common.jsr303.MinioBucket;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;

/**
 * 文件 Service
 *
 * @author seliote
 */
@Validated
public interface FileService {

    // 图片分类名称
    String IMG = "img";
    // 音频分类名称
    String AUDIO = "audio";

    /**
     * 上传文件到指定目录
     * <em>操作完成后不会关闭流, 需调用者来关闭</em>
     * <em>未正确关闭流会导致句柄泄漏</em>
     *
     * @param catalog     分类
     * @param filename    文件名称
     * @param inputStream 输入流
     */
    void upload(@MinioBucket String catalog,
                @NotBlank String filename,
                @NotNull InputStream inputStream) throws IOException;

    /**
     * 下载指定文件
     * <em>需调用方关闭返回的流</em>
     * <em>未正确关闭流会导致句柄泄漏</em>
     *
     * @param catalog  分类
     * @param filename 文件名称
     */
    InputStream download(@MinioBucket String catalog, @NotBlank String filename) throws IOException;
}

Impl

package com.seliote.mlb.fs.service.impl;

import com.seliote.mlb.common.config.PropertiesInjector;
import com.seliote.mlb.fs.service.FileService;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * 文件 Service 实现
 *
 * @author seliote
 */
@Slf4j
@Service
public class FileServiceImpl implements FileService {

    // 分类间的分隔符
    private final String CATALOG_SEPARATOR = "/";

    private final PropertiesInjector.Minio minioProp;
    private final MinioClient minioClient;

    @Autowired
    public FileServiceImpl(PropertiesInjector propertiesInjector,
                           MinioClient minioClient) {
        this.minioProp = propertiesInjector.getMinio();
        this.minioClient = minioClient;
    }

    @Override
    public void upload(String catalog, String filename, InputStream inputStream) throws IOException {
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(minioProp.getBucket())
                    .object(String.format("%s%s%s", catalog, CATALOG_SEPARATOR, filename))
                    .stream(inputStream, -1, 1024 * 1024 * 10)
                    .build());
            log.info("Upload {} {} success", catalog, filename);
        } catch (MinioException | InvalidKeyException | NoSuchAlgorithmException e) {
            log.error("Upload {} {} failed, {}, message: {}",
                    catalog, filename, e.getClass().getCanonicalName(), e.getMessage());
            throw new IOException(e);
        }
    }

    @Override
    public InputStream download(String catalog, String filename) throws IOException {
        try {
            var getObjectResponse = minioClient.getObject(GetObjectArgs.builder()
                    .bucket(minioProp.getBucket())
                    .object(String.format("%s%s%s", catalog, CATALOG_SEPARATOR, filename))
                    .build());
            log.info("Download {} {} success", catalog, filename);
            return getObjectResponse;
        } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
            log.error("Download {} {} failed, {}, message: {}",
                    catalog, filename, e.getClass().getCanonicalName(), e.getMessage());
            throw new IOException(e);
        }
    }
}

Java MinIO 集成

标签:accesskey   package   beans   分隔符   bool   ota   ica   ice   cli   

原文地址:https://www.cnblogs.com/seliote/p/14828822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!