菜鸟关于SpringBoot配置MinIo的一些疑惑问题的记录

  • Post author:
  • Post category:其他


(本文章完全仅适合什么都不懂的小白阅读…….)在项目中运用到了MinIo,因为后台用了springboot,所以就搜索了关于springboot配置MinIo的一些博文(本人对于springboot的原理并不了解,仅局限于依靠百度可以使用的程度),在MinIO的配置上做了如下工作:

首先在application.yml中配置了minIo的连接信息:

#minIO 地址
minio:
  endpoint: http://127.0.0.1
  port: 9000
  accessKey: minioadmin
  secretKey: minioadmin
  secure: false

然后写了配置类(这时我也并不了解这个叫法的意义):

MinioPropertiesConfig.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * minio配置类
 *
 */
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioPropertiesConfig {

    /**
     * 端点
     */
    private String endpoint;
    
    /**
     * 端口
     */
    private int port;
    /**
     * 用户名
     */
    private String accessKey;
    /**
     * 密码
     */
    private String secretKey;

    /**
     * 桶名称
     */
    private String bucketName;
    
    /**
     * 开启https
     */
    private Boolean secure;

	public String getEndpoint() {
		return endpoint;
	}

	public void setEndpoint(String endpoint) {
		this.endpoint = endpoint;
	}

	public String getAccessKey() {
		return accessKey;
	}

	public void setAccessKey(String accessKey) {
		this.accessKey = accessKey;
	}

	public String getSecretKey() {
		return secretKey;
	}

	public void setSecretKey(String secretKey) {
		this.secretKey = secretKey;
	}

	public String getBucketName() {
		return bucketName;
	}

	public void setBucketName(String bucketName) {
		this.bucketName = bucketName;
	}

	public Boolean getSecure() {
		return secure;
	}

	public void setSecure(Boolean secure) {
		this.secure = secure;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
    
}

MinioConfig.java

@Configuration
@EnableConfigurationProperties(MinioPropertiesConfig.class)
public class MinioConfig{
	@Resource
    private MinioPropertiesConfig minioPropertiesConfig;
    
    @Bean
    public MinioClient getMinioClient() throws InvalidEndpointException, InvalidPortException {
        MinioClient minioClient = MinioClient.builder().endpoint(minioPropertiesConfig.getEndpoint(), minioPropertiesConfig.getPort(), minioPropertiesConfig.getSecure())
                        .credentials(minioPropertiesConfig.getAccessKey(), minioPropertiesConfig.getSecretKey())
                        .build();
        return minioClient;
    }
    
}

最后直接在我的工具类中就可以直接调用:

MinioClientUtils.java

@Component
public class MinioClientUtils {

    @Autowired
    private MinioClient minioClient;

    /**
     * 判断bucket是否存在,不存在则创建
     *
     * @return: void
     */
    public void existBucket(String name) {
        try {
            boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());
            if (!exists) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build());
                //默认设置桶策越为public
                String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
                        + "\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:ListBucketMultipartUploads\","
                        + "\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::"
                        + name + "\"]},"
                        + "{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},"
                        + "\"Action\":[\"s3:AbortMultipartUpload\",\"s3:DeleteObject\",\"s3:GetObject\","
                        + "\"s3:ListMultipartUploadParts\",\"s3:PutObject\"],"
                        + "\"Resource\":[\"arn:aws:s3:::" + name + "/*\"]}]}";
                minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().config(policy).bucket(name).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

按上述的方法配置后,使用上是没问题,但对于菜鸟的我来说这时候问题就来了,minioClient是怎么被自动配置上的呢

,经过一顿查找和试验懵懂的了解了点点懵懂的东西

,情况是这样滴,请听我娓娓道来…….

我在MinioConfig.java这个文件中使用了@Configuration和@Bean这两个标注,springboot会在

启动的时候

把@Bean托管(自动执行 暂且这么理解),然后再MinioClientUtils.java中我使用了@Autowired标注,它会自动去找寻一遍被@Bean标注的对象(返回类型一致的,我这里是bean里面返回了MinioClient的类型),查找到返回类型一致的对象即把它给抢过来复制类似于MinioClient minioClient = getMinioClient();这么个过程,因此minioClient就被自动注入了。画重点,@Autowired会去查找被@Bean标注的返回类型一致的对象来进行注入。你可以自行测试,写一个@Autowired,然后再写一个@Configuration类,里面写一个@Bean。如果没有@Autowired找不到配对的@Bean启动时就会出现

Field minioClient in xxx required a bean of type 'xxx' that could not be found.

的错误。



版权声明:本文为lg8883573原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。