com.taotao.boot.oss.wangyi.WangYiOssConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taotao-boot-starter-oss-wangyi Show documentation
Show all versions of taotao-boot-starter-oss-wangyi Show documentation
taotao-boot-starter-oss-wangyi
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.wangyi;
import com.netease.cloud.auth.BasicCredentials;
import com.netease.cloud.auth.Credentials;
import com.netease.cloud.services.nos.NosClient;
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 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.ConditionalOnClass;
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;
import java.util.Map;
/**
* 王毅oss配置
*
* @author shuigedeng
* @version 2022.04
* @since 2022-04-27 17:44:06
*/
@AutoConfiguration
@ConditionalOnOssEnabled
@ConditionalOnClass(NosClient.class)
@EnableConfigurationProperties({WangYiOssProperties.class})
@ConditionalOnProperty(prefix = OssProperties.PREFIX, name = "type", havingValue = "wangyi")
public class WangYiOssConfiguration implements InitializingBean {
public WangYiOssConfiguration(WangYiOssProperties wangYiOssProperties) {
this.wangYiOssProperties = wangYiOssProperties;
}
@Override
public void afterPropertiesSet() throws Exception {
LogUtils.started(WangYiOssConfiguration.class, StarterName.OSS_WANGYI_STARTER);
}
public static final String DEFAULT_BEAN_NAME = "wangYiOssClient";
private final WangYiOssProperties wangYiOssProperties;
@Bean
@ConditionalOnMissingBean
public StandardOssClient wangYiOssClient() {
Map wangYiOssConfigMap = wangYiOssProperties.getOssConfig();
if (wangYiOssConfigMap.isEmpty()) {
SpringUtil.registerBean(DEFAULT_BEAN_NAME, wangYiOssClient(wangYiOssProperties));
} else {
String endPoint = wangYiOssProperties.getEndpoint();
String accessKey = wangYiOssProperties.getAccessKey();
String secretKey = wangYiOssProperties.getSecretKey();
wangYiOssConfigMap.forEach((name, wangYiOssConfig) -> {
if (ObjUtil.isEmpty(wangYiOssConfig.getEndpoint())) {
wangYiOssConfig.setEndpoint(endPoint);
}
if (ObjUtil.isEmpty(wangYiOssConfig.getAccessKey())) {
wangYiOssConfig.setAccessKey(accessKey);
}
if (ObjUtil.isEmpty(wangYiOssConfig.getSecretKey())) {
wangYiOssConfig.setSecretKey(secretKey);
}
SpringUtil.registerBean(name, wangYiOssClient(wangYiOssConfig));
});
}
return null;
}
public StandardOssClient wangYiOssClient(WangYiOssConfig ossConfig) {
return new WangYiOssClient(nosClient(ossConfig), ossConfig);
}
public NosClient nosClient(WangYiOssConfig ossConfig) {
Credentials credentials = new BasicCredentials(ossConfig.getAccessKey(), ossConfig.getSecretKey());
NosClient nosClient = new NosClient(credentials);
nosClient.setEndpoint(ossConfig.getEndpoint());
nosClient.setConfiguration(ossConfig.getClientConfig());
return nosClient;
}
}