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

com.taotao.cloud.oss.pingan.PingAnOssConfiguration 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.pingan;

import com.pingan.radosgw.sdk.config.ObsClientConfig;
import com.pingan.radosgw.sdk.service.RadosgwService;
import com.pingan.radosgw.sdk.service.RadosgwServiceFactory;
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.exception.OssException;
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:42:08
 */
@AutoConfiguration
@ConditionalOnOssEnabled
@EnableConfigurationProperties({PingAnOssProperties.class})
@ConditionalOnProperty(prefix = OssProperties.PREFIX, name = "type", havingValue = "pingan")
public class PingAnOssConfiguration implements InitializingBean {

	public PingAnOssConfiguration(PingAnOssProperties pingAnOssProperties) {
		this.pingAnOssProperties = pingAnOssProperties;
	}

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

    public static final String DEFAULT_BEAN_NAME = "pingAnOssClient";

    private final PingAnOssProperties pingAnOssProperties;

    @Bean
    @ConditionalOnMissingBean
    public StandardOssClient pingAnOssClient() {
        Map ossConfigMap = pingAnOssProperties.getOssConfig();
        if (ossConfigMap.isEmpty()) {
            SpringUtil.registerBean(DEFAULT_BEAN_NAME, pingAnOssClient(pingAnOssProperties));
        } else {
            String userAgent = pingAnOssProperties.getUserAgent();
            String obsUrl = pingAnOssProperties.getObsUrl();
            String obsAccessKey = pingAnOssProperties.getObsAccessKey();
            String obsSecret = pingAnOssProperties.getObsSecret();
            String domainName = pingAnOssProperties.getDomainName();
            ossConfigMap.forEach((name, ossConfig) -> {
                if (ObjUtil.isEmpty(ossConfig.getUserAgent())) {
                    ossConfig.setUserAgent(userAgent);
                }
                if (ObjUtil.isEmpty(ossConfig.getObsUrl())) {
                    ossConfig.setObsUrl(obsUrl);
                }
                if (ObjUtil.isEmpty(ossConfig.getObsAccessKey())) {
                    ossConfig.setObsAccessKey(obsAccessKey);
                }
                if (ObjUtil.isEmpty(ossConfig.getObsSecret())) {
                    ossConfig.setObsSecret(obsSecret);
                }
                if (ObjUtil.isEmpty(ossConfig.getDomainName())) {
                    ossConfig.setDomainName(domainName);
                }
                SpringUtil.registerBean(name, pingAnOssClient(ossConfig));
            });
        }
        return null;
    }

    public StandardOssClient pingAnOssClient(PingAnOssConfig pingAnOssConfig) {
        return new PingAnOssClient(pingAnOssConfig, radosgwService(pingAnOssConfig));
    }

    public RadosgwService radosgwService(PingAnOssConfig pingAnOssConfig) {
        ObsClientConfig oc = new ObsClientConfig() {
            public String getUserAgent() {
                return pingAnOssConfig.getUserAgent();
            }

            public String getObsUrl() {
                return pingAnOssConfig.getObsUrl();
            }

            public String getObsAccessKey() {
                return pingAnOssConfig.getObsAccessKey();
            }

            public String getObsSecret() {
                return pingAnOssConfig.getObsSecret();
            }

            @Override
            public boolean isRepresentPathInKey() {
                return pingAnOssConfig.getRepresentPathInKey();
            }
        };

        try {
            String domainName = pingAnOssConfig.getDomainName();
            if (ObjUtil.isEmpty(domainName)) {
                return RadosgwServiceFactory.getFromConfigObject(oc);
            } else {
                return RadosgwServiceFactory.getFromConfigObject(oc, domainName);
            }
        } catch (Exception e) {
            throw new OssException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy