org.zodiac.autoconfigure.oss.PlatformTencentCosAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.oss;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.zodiac.autoconfigure.oss.condition.ConditionalOnOssEnabled;
import org.zodiac.autoconfigure.security.condition.ConditionalOnSecurityHttpEnabled;
/**
*
* 腾讯云 COS 自动装配。
*
*
*/
@SpringBootConfiguration
@ConditionalOnOssEnabled
//@ConditionalOnWebApplication
@ConditionalOnSecurityHttpEnabled
@AutoConfigureAfter(value = {PlatformOssAutoConfiguration.class})
@ConditionalOnClass(value = {com.qcloud.cos.COSClient.class, org.zodiac.oss.config.PlatformOssInfo.class})
@ConditionalOnProperty(value = org.zodiac.oss.constants.OssSystemPropertiesConstants.PLATFORM_OSS_NAME, havingValue = org.zodiac.oss.constants.OssConstants.TENCENT_COS)
public class PlatformTencentCosAutoConfiguration {
private final PlatformOssProperties ossProperties;
private final org.zodiac.oss.rule.OssRule ossRule;
public PlatformTencentCosAutoConfiguration(PlatformOssProperties ossProperties, org.zodiac.oss.rule.OssRule ossRule) {
this.ossProperties = ossProperties;
this.ossRule = ossRule;
}
@Bean
@ConditionalOnMissingBean
protected com.qcloud.cos.COSClient ossClient() {
/*初始化用户身份信息(secretId, secretKey)*/
com.qcloud.cos.auth.COSCredentials credentials =
new com.qcloud.cos.auth.BasicCOSCredentials(ossProperties.getAccessKey(), ossProperties.getSecretKey());
/*设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224*/
com.qcloud.cos.region.Region region = new com.qcloud.cos.region.Region(ossProperties.getRegion());
/*clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。*/
com.qcloud.cos.ClientConfig clientConfig = new com.qcloud.cos.ClientConfig(region);
org.zodiac.oss.config.TencentCosInfo tencentCos = ossProperties.getTencentCos();
clientConfig.setMaxConnectionsCount(tencentCos.getMaxConnectionsCount());
clientConfig.setSocketTimeout(tencentCos.getSocketTimeout());
clientConfig.setConnectionTimeout(tencentCos.getConnectionTimeout());
clientConfig.setConnectionRequestTimeout(tencentCos.getConnectionRequestTimeout());
return new com.qcloud.cos.COSClient(credentials, clientConfig);
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(value = {com.qcloud.cos.COSClient.class})
protected org.zodiac.oss.TencentCosTemplate tencentCosTemplate(com.qcloud.cos.COSClient cosClient) {
return new org.zodiac.oss.TencentCosTemplate(cosClient, ossProperties, ossRule);
}
}