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

com.taotao.boot.oss.qingyun.QingYunOssConfiguration Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.oss.qingyun;

import com.qingstor.sdk.config.EnvContext;
import com.qingstor.sdk.service.Bucket;
import com.qingstor.sdk.service.QingStor;
import com.taotao.boot.common.constant.StarterName;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.oss.common.condition.ConditionalOnOssEnabled;
import com.taotao.boot.oss.common.propeties.OssProperties;
import com.taotao.boot.oss.common.service.StandardOssClient;
import java.util.Map;

import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.extra.spring.SpringUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * 清云操作系统配置
 *
 * @author shuigedeng
 * @version 2022.04
 * @since 2022-04-27 17:42:19
 */
@AutoConfiguration
@ConditionalOnOssEnabled
@EnableConfigurationProperties({QingYunOssProperties.class})
@ConditionalOnProperty(prefix = OssProperties.PREFIX, name = "type", havingValue = "qingyun")
public class QingYunOssConfiguration implements InitializingBean {

	public QingYunOssConfiguration(QingYunOssProperties qingYunOssProperties) {
		this.qingYunOssProperties = qingYunOssProperties;
	}

	@Override
    public void afterPropertiesSet() throws Exception {
        LogUtils.started(QingYunOssConfiguration.class, StarterName.OSS_QINGYUN_STARTER);
    }

    public static final String DEFAULT_BEAN_NAME = "qingYunOssClient";

    private final QingYunOssProperties qingYunOssProperties;

    @Bean
    @ConditionalOnMissingBean
    public StandardOssClient qingYunOssClient() {
        Map ossConfigMap = qingYunOssProperties.getOssConfig();
        if (ossConfigMap.isEmpty()) {
            SpringUtil.registerBean(DEFAULT_BEAN_NAME, qingYunOssClient(qingYunOssProperties));
        } else {
            String endpoint = qingYunOssProperties.getEndpoint();
            String accessKey = qingYunOssProperties.getAccessKey();
            String accessSecret = qingYunOssProperties.getAccessSecret();
            String zone = qingYunOssProperties.getZone();
            EnvContext.HttpConfig clientConfig = qingYunOssProperties.getClientConfig();
            ossConfigMap.forEach((name, ossConfig) -> {
                if (ObjUtil.isEmpty(ossConfig.getEndpoint())) {
                    ossConfig.setEndpoint(endpoint);
                }
                if (ObjUtil.isEmpty(ossConfig.getAccessKey())) {
                    ossConfig.setAccessKey(accessKey);
                }
                if (ObjUtil.isEmpty(ossConfig.getAccessSecret())) {
                    ossConfig.setAccessSecret(accessSecret);
                }
                if (ObjUtil.isEmpty(ossConfig.getZone())) {
                    ossConfig.setZone(zone);
                }
                if (ObjUtil.isEmpty(ossConfig.getClientConfig())) {
                    ossConfig.setClientConfig(clientConfig);
                }
                SpringUtil.registerBean(name, qingYunOssClient(ossConfig));
            });
        }
        return null;
    }

    public StandardOssClient qingYunOssClient(QingYunOssConfig qingYunOssConfig) {
        QingStor qingStor = qingStor(qingYunOssConfig);
        Bucket bucket = qingStor.getBucket(qingYunOssConfig.getBucketName(), qingYunOssConfig.getZone());
        return new QingYunOssClient(qingStor, bucket, qingYunOssConfig);
    }

    public QingStor qingStor(QingYunOssConfig qingYunOssConfig) {
        EnvContext env = new EnvContext(qingYunOssConfig.getAccessKey(), qingYunOssConfig.getAccessSecret());
        env.setHttpConfig(qingYunOssConfig.getClientConfig());
        String endpoint = qingYunOssConfig.getEndpoint();
        if (ObjUtil.isNotEmpty(endpoint)) {
            env.setEndpoint(endpoint);
        }
        env.setCnameSupport(qingYunOssConfig.getCnameSupport());
        env.setAdditionalUserAgent(qingYunOssConfig.getAdditionalUserAgent());
        env.setVirtualHostEnabled(qingYunOssConfig.getVirtualHostEnabled());
        return new QingStor(env);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy