com.taotao.boot.oss.ucloud.UCloudOssConfiguration 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-ucloud Show documentation
Show all versions of taotao-boot-starter-oss-ucloud Show documentation
taotao-boot-starter-oss-ucloud
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.ucloud;
import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.object.ObjectApiBuilder;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.auth.ObjectAuthorization;
import cn.ucloud.ufile.auth.UfileObjectLocalAuthorization;
import cn.ucloud.ufile.http.HttpClient;
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;
/**
* ucloud oss配置
*
* @author shuigedeng
* @version 2022.04
* @since 2022-04-27 17:43:29
*/
@AutoConfiguration
@ConditionalOnOssEnabled
@ConditionalOnClass(UfileClient.class)
@EnableConfigurationProperties({UCloudOssProperties.class})
@ConditionalOnProperty(prefix = OssProperties.PREFIX, name = "type", havingValue = "ucloud")
public class UCloudOssConfiguration implements InitializingBean {
public UCloudOssConfiguration(UCloudOssProperties uCloudOssProperties) {
this.uCloudOssProperties = uCloudOssProperties;
}
@Override
public void afterPropertiesSet() throws Exception {
LogUtils.started(UCloudOssConfiguration.class, StarterName.OSS_UCLOUD_STARTER);
}
public static final String DEFAULT_BEAN_NAME = "uCloudOssClient";
private final UCloudOssProperties uCloudOssProperties;
@Bean
@ConditionalOnMissingBean
public StandardOssClient uCloudOssClient() {
Map ossConfigMap = uCloudOssProperties.getOssConfig();
if (ossConfigMap.isEmpty()) {
SpringUtil.registerBean(DEFAULT_BEAN_NAME, uCloudOssClient(uCloudOssProperties));
} else {
String publicKey = uCloudOssProperties.getPublicKey();
String privateKey = uCloudOssProperties.getPrivateKey();
String region = uCloudOssProperties.getRegion();
String proxySuffix = uCloudOssProperties.getProxySuffix();
HttpClient.Config clientConfig = uCloudOssProperties.getClientConfig();
ossConfigMap.forEach((name, ossConfig) -> {
if (ObjUtil.isEmpty(ossConfig.getPublicKey())) {
ossConfig.setPublicKey(publicKey);
}
if (ObjUtil.isEmpty(ossConfig.getPrivateKey())) {
ossConfig.setPrivateKey(privateKey);
}
if (ObjUtil.isEmpty(ossConfig.getRegion())) {
ossConfig.setRegion(region);
}
if (ObjUtil.isEmpty(ossConfig.getProxySuffix())) {
ossConfig.setProxySuffix(proxySuffix);
}
if (ObjUtil.isEmpty(ossConfig.getClientConfig())) {
ossConfig.setClientConfig(clientConfig);
}
SpringUtil.registerBean(name, uCloudOssClient(ossConfig));
});
}
return null;
}
public StandardOssClient uCloudOssClient(UCloudOssConfig uCloudOssConfig) {
UfileClient.Config config = new UfileClient.Config(uCloudOssConfig.getClientConfig());
ObjectAuthorization objectAuthorization =
new UfileObjectLocalAuthorization(uCloudOssConfig.getPublicKey(), uCloudOssConfig.getPrivateKey());
ObjectConfig objectConfig = new ObjectConfig(uCloudOssConfig.getRegion(), uCloudOssConfig.getProxySuffix());
UfileClient ufileClient = UfileClient.configure(config);
ObjectApiBuilder objectApiBuilder = new ObjectApiBuilder(ufileClient, objectAuthorization, objectConfig);
return new UCloudOssClient(ufileClient, objectApiBuilder, uCloudOssConfig);
}
}