All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.fscode.commons.minio.autoconfig.MinioConfig Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package cn.fscode.commons.minio.autoconfig;

import cn.fscode.commons.minio.client.CustomMinioClient;
import cn.fscode.commons.minio.utils.MinioUtils;
import cn.fscode.commons.storage.api.config.MinioStorageProperties;
import cn.fscode.commons.storage.api.config.StorageProperties;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Description: minio配置类,如果服务器与客户端主机的时间相差太大会启动失败
 *
 * @author shenguangyang
 */
@Configuration
@ConditionalOnProperty(prefix = "storage", name = "type", havingValue = "minio")
public class MinioConfig {
    /**
     * logger
     */
    private final static Logger log = LoggerFactory.getLogger(MinioConfig.class);

    @Resource
    private StorageProperties storageProperties;

    @Resource
    private MinioStorageProperties minioStorageProperties;

    @PostConstruct
    public void init() throws Exception{
        log.info("Minio文件系统初始化加载");

        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint(minioStorageProperties.getEndpoint())
                        .credentials(minioStorageProperties.getAccessKey(), minioStorageProperties.getSecretKey())
                        .build();
        if (storageProperties.getInitBucket()) {
            // 判断Bucket是否存在
            String bucketName = minioStorageProperties.getBucketName();
            boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if(isExist) {
                log.info("Minio文件系统Bucket: {} 已存在", bucketName);
            } else {
                // 不存在创建一个新的Bucket
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                log.info("Minio已创建Bucket: {}", bucketName);
            }
        }

        CustomMinioClient customMinioClient = new CustomMinioClient(minioClient);
        MinioUtils.init(customMinioClient, minioStorageProperties);
        log.info("Minio文件系统初始化完成");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy