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

cn.soilove.oss.config.OssAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package cn.soilove.oss.config;

import cn.soilove.oss.properties.OssProperties;
import cn.soilove.oss.service.OssService;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * aliyun oss 自动配置类
 *
 * @author: Chen GuoLin
 * @create: 2020-04-07 18:00
 **/
@Configuration
@ConditionalOnClass(OssService.class)
@EnableConfigurationProperties(OssProperties.class)
public class OssAutoConfiguration {

    @Resource
    private OssProperties ossProperties;

    @Bean
    @ConditionalOnMissingBean
    public OSS ossClient() {
        // 检查参数
        if (StringUtils.isEmpty(ossProperties.getAccessKeyId())
                || StringUtils.isEmpty(ossProperties.getAccessKeySecret())
                || StringUtils.isEmpty(ossProperties.getBucket())
                || StringUtils.isEmpty(ossProperties.getEndpoint())) {
            throw new OssStarterException("[oss][starter]OSS配置缺失,请补充");
        }

        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());

        return ossClient;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy