com.taotao.cloud.oss.jinshan.JinShanOssConfiguration 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.jinshan;
import com.ksyun.ks3.service.Ks3;
import com.ksyun.ks3.service.Ks3Client;
import com.ksyun.ks3.service.Ks3ClientConfig;
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;
/**
* 金山开源软件配置
*
* @author shuigedeng
* @version 2022.04
* @since 2022-04-27 17:41:20
*/
@AutoConfiguration
@ConditionalOnOssEnabled
@EnableConfigurationProperties({JinShanOssProperties.class})
@ConditionalOnProperty(prefix = OssProperties.PREFIX, name = "type", havingValue = "jinshan")
public class JinShanOssConfiguration implements InitializingBean {
public JinShanOssConfiguration(JinShanOssProperties jinShanOssProperties) {
this.jinShanOssProperties = jinShanOssProperties;
}
@Override
public void afterPropertiesSet() throws Exception {
LogUtils.started(JinShanOssConfiguration.class, StarterName.OSS_JINSHAN_STARTER);
}
public static final String DEFAULT_BEAN_NAME = "jinShanOssClient";
private final JinShanOssProperties jinShanOssProperties;
@Bean
@ConditionalOnMissingBean
public StandardOssClient jinShanOssClient() {
Map ossConfigMap = jinShanOssProperties.getOssConfig();
if (ossConfigMap.isEmpty()) {
SpringUtil.registerBean(DEFAULT_BEAN_NAME, jinShanOssClient(jinShanOssProperties));
} else {
String accessKeyId = jinShanOssProperties.getAccessKeyId();
String accessKeySecret = jinShanOssProperties.getAccessKeySecret();
Ks3ClientConfig clientConfig = jinShanOssProperties.getClientConfig();
ossConfigMap.forEach((name, jinShanOssConfig) -> {
if (ObjUtil.isEmpty(jinShanOssConfig.getAccessKeyId())) {
jinShanOssConfig.setAccessKeyId(accessKeyId);
}
if (ObjUtil.isEmpty(jinShanOssConfig.getAccessKeySecret())) {
jinShanOssConfig.setAccessKeySecret(accessKeySecret);
}
if (ObjUtil.isEmpty(jinShanOssConfig.getClientConfig())) {
jinShanOssConfig.setClientConfig(clientConfig);
}
SpringUtil.registerBean(name, jinShanOssClient(jinShanOssConfig));
});
}
return null;
}
public StandardOssClient jinShanOssClient(JinShanOssConfig jinShanOssConfig) {
return new JinShanOssClient(ks3(jinShanOssConfig), jinShanOssConfig);
}
public Ks3 ks3(JinShanOssConfig ossConfig) {
Ks3ClientConfig clientConfig = ossConfig.getClientConfig();
return new Ks3Client(ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret(), clientConfig);
}
}