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

com.taotao.cloud.oss.huawei.HuaWeiOssConfiguration Maven / Gradle / Ivy

/*
 * 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.cloud.oss.huawei;

import com.obs.services.ObsClient;
import com.obs.services.ObsConfiguration;
import com.taotao.cloud.common.constant.StarterName;
import com.taotao.cloud.common.utils.log.LogUtils;
import com.taotao.cloud.oss.common.condition.ConditionalOnOssEnabled;
import com.taotao.cloud.oss.common.propeties.OssProperties;
import com.taotao.cloud.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.beans.factory.annotation.Autowired;
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;

/**
 * 华魏oss配置
 *
 * @author shuigedeng
 * @version 2022.04
 * @since 2022-04-27 17:40:24
 */
@AutoConfiguration
@ConditionalOnOssEnabled
@EnableConfigurationProperties({HuaWeiOssProperties.class})
@ConditionalOnProperty(prefix = OssProperties.PREFIX, name = "type", havingValue = "huawei")
public class HuaWeiOssConfiguration implements InitializingBean {

	public HuaWeiOssConfiguration(HuaWeiOssProperties huaWeiOssProperties) {
		this.huaWeiOssProperties = huaWeiOssProperties;
	}

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

    public static final String DEFAULT_BEAN_NAME = "huaWeiOssClient";

    private final HuaWeiOssProperties huaWeiOssProperties;

    @Bean
    @ConditionalOnMissingBean
    public StandardOssClient huaWeiOssClient() {
        Map huaweiOssConfigMap = huaWeiOssProperties.getOssConfig();
        if (huaweiOssConfigMap.isEmpty()) {
            SpringUtil.registerBean(DEFAULT_BEAN_NAME, huaWeiOssClient(huaWeiOssProperties));
        } else {
            String accessKey = huaWeiOssProperties.getAccessKey();
            String secretKey = huaWeiOssProperties.getSecretKey();
            ObsConfiguration clientConfig = huaWeiOssProperties.getClientConfig();
            huaweiOssConfigMap.forEach((name, huaweiOssConfig) -> {
                if (ObjUtil.isEmpty(huaweiOssConfig.getAccessKey())) {
                    huaweiOssConfig.setAccessKey(accessKey);
                }
                if (ObjUtil.isEmpty(huaweiOssConfig.getSecretKey())) {
                    huaweiOssConfig.setSecretKey(secretKey);
                }
                if (ObjUtil.isEmpty(huaweiOssConfig.getClientConfig())) {
                    huaweiOssConfig.setClientConfig(clientConfig);
                }
                SpringUtil.registerBean(name, huaWeiOssClient(huaweiOssConfig));
            });
        }
        return null;
    }

    public StandardOssClient huaWeiOssClient(HuaweiOssConfig huaweiOssConfig) {
        return new HuaWeiOssClient(obsClient(huaweiOssConfig), huaweiOssConfig);
    }

    public ObsClient obsClient(HuaweiOssConfig huaweiOssConfig) {
        return new ObsClient(
                huaweiOssConfig.getAccessKey(), huaweiOssConfig.getSecretKey(), huaweiOssConfig.getClientConfig());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy