🎉 更新oss模块注释

This commit is contained in:
smallchill 2025-02-28 01:03:07 +08:00
parent 0d8accc18f
commit e360b0b916
10 changed files with 323 additions and 110 deletions

View File

@ -15,14 +15,13 @@
*/ */
package org.springblade.core.oss; package org.springblade.core.oss;
import com.aliyun.oss.OSSClient; import java.io.InputStream;
import com.aliyun.oss.common.utils.BinaryUtil; import java.nio.charset.StandardCharsets;
import com.aliyun.oss.model.MatchMode; import java.util.Date;
import com.aliyun.oss.model.ObjectMetadata; import java.util.LinkedHashMap;
import com.aliyun.oss.model.PolicyConditions; import java.util.List;
import com.aliyun.oss.model.PutObjectResult; import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.core.oss.model.BladeFile; import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.oss.model.OssFile; import org.springblade.core.oss.model.OssFile;
import org.springblade.core.oss.props.OssProperties; import org.springblade.core.oss.props.OssProperties;
@ -33,12 +32,15 @@ import org.springblade.core.tool.utils.StringUtil;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream; import com.aliyun.oss.OSSClient;
import java.nio.charset.StandardCharsets; import com.aliyun.oss.common.utils.BinaryUtil;
import java.util.Date; import com.aliyun.oss.model.MatchMode;
import java.util.LinkedHashMap; import com.aliyun.oss.model.ObjectMetadata;
import java.util.List; import com.aliyun.oss.model.PolicyConditions;
import java.util.Map; import com.aliyun.oss.model.PutObjectResult;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
/** /**
* AliossTemplate * AliossTemplate
@ -157,6 +159,15 @@ public class AliossTemplate implements OssTemplate {
return put(bucketName, stream, fileName, false); return put(bucketName, stream, fileName, false);
} }
/**
* 上传文件到OSS
*
* @param bucketName 存储桶名称
* @param stream 输入流
* @param key 文件名
* @param cover 是否覆盖上传
* @return BladeFile 上传文件信息
*/
@SneakyThrows @SneakyThrows
public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) { public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) {
makeBucket(bucketName); makeBucket(bucketName);
@ -207,48 +218,61 @@ public class AliossTemplate implements OssTemplate {
} }
/** /**
* 根据规则生成存储桶名称规则 * 获取默认存储桶名称
* *
* @return String * @return String 存储桶名称
*/ */
private String getBucketName() { private String getBucketName() {
return getBucketName(ossProperties.getBucketName()); return getBucketName(ossProperties.getBucketName());
} }
/** /**
* 根据规则生成存储桶名称规则 * 根据规则生成存储桶名称
* *
* @param bucketName 存储桶名称 * @param bucketName 存储桶名称
* @return String * @return String 处理后的存储桶名称
*/ */
private String getBucketName(String bucketName) { private String getBucketName(String bucketName) {
return ossRule.bucketName(bucketName); return ossRule.bucketName(bucketName);
} }
/** /**
* 根据规则生成文件名称规则 * 根据规则生成文件名称
* *
* @param originalFilename 原始文件名 * @param originalFilename 原始文件名
* @return string * @return String 处理后的文件名
*/ */
private String getFileName(String originalFilename) { private String getFileName(String originalFilename) {
return ossRule.fileName(originalFilename); return ossRule.fileName(originalFilename);
} }
/**
* 获取默认存储桶的上传凭证
*
* @return String 上传凭证
*/
public String getUploadToken() { public String getUploadToken() {
return getUploadToken(ossProperties.getBucketName()); return getUploadToken(ossProperties.getBucketName());
} }
/** /**
* TODO 过期时间 * 获取指定存储桶的上传凭证
* <p> *
* 获取上传凭证普通上传 * @param bucketName 存储桶名称
* @return String 上传凭证
*/ */
public String getUploadToken(String bucketName) { public String getUploadToken(String bucketName) {
// 默认过期时间2小时 // 默认过期时间2小时
return getUploadToken(bucketName, ossProperties.getArgs().get("expireTime", 3600L)); return getUploadToken(bucketName, ossProperties.getArgs().get("expireTime", 3600L));
} }
/**
* 获取带过期时间的上传凭证
*
* @param bucketName 存储桶名称
* @param expireTime 过期时间
* @return String 上传凭证
*/
public String getUploadToken(String bucketName, long expireTime) { public String getUploadToken(String bucketName, long expireTime) {
String baseDir = "upload"; String baseDir = "upload";
@ -276,10 +300,10 @@ public class AliossTemplate implements OssTemplate {
} }
/** /**
* 获取域名 * 获取指定存储桶的OSS访问域名
* *
* @param bucketName 存储桶名称 * @param bucketName 存储桶名称
* @return String * @return String OSS访问域名
*/ */
public String getOssHost(String bucketName) { public String getOssHost(String bucketName) {
String prefix = getEndpoint().contains("https://") ? "https://" : "http://"; String prefix = getEndpoint().contains("https://") ? "https://" : "http://";
@ -287,18 +311,18 @@ public class AliossTemplate implements OssTemplate {
} }
/** /**
* 获取域名 * 获取默认存储桶的OSS访问域名
* *
* @return String * @return String OSS访问域名
*/ */
public String getOssHost() { public String getOssHost() {
return getOssHost(ossProperties.getBucketName()); return getOssHost(ossProperties.getBucketName());
} }
/** /**
* 获取服务地址 * 获取OSS服务的Endpoint
* *
* @return String * @return String OSS Endpoint
*/ */
public String getEndpoint() { public String getEndpoint() {
if (StringUtil.isBlank(ossProperties.getTransformEndpoint())) { if (StringUtil.isBlank(ossProperties.getTransformEndpoint())) {
@ -307,5 +331,4 @@ public class AliossTemplate implements OssTemplate {
return ossProperties.getTransformEndpoint(); return ossProperties.getTransformEndpoint();
} }
} }

View File

@ -78,17 +78,33 @@ public class MinioTemplate implements OssTemplate {
} }
} }
/**
* 获取默认存储桶
*
* @return Bucket MinIO存储桶
*/
@SneakyThrows @SneakyThrows
public Bucket getBucket() { public Bucket getBucket() {
return getBucket(getBucketName()); return getBucket(getBucketName());
} }
/**
* 获取指定名称的存储桶
*
* @param bucketName 存储桶名称
* @return Bucket MinIO存储桶
*/
@SneakyThrows @SneakyThrows
public Bucket getBucket(String bucketName) { public Bucket getBucket(String bucketName) {
Optional<Bucket> bucketOptional = client.listBuckets().stream().filter(bucket -> bucket.name().equals(getBucketName(bucketName))).findFirst(); Optional<Bucket> bucketOptional = client.listBuckets().stream().filter(bucket -> bucket.name().equals(getBucketName(bucketName))).findFirst();
return bucketOptional.orElse(null); return bucketOptional.orElse(null);
} }
/**
* 获取所有存储桶列表
*
* @return 存储桶列表
*/
@SneakyThrows @SneakyThrows
public List<Bucket> listBuckets() { public List<Bucket> listBuckets() {
return client.listBuckets(); return client.listBuckets();
@ -202,6 +218,15 @@ public class MinioTemplate implements OssTemplate {
return putFile(bucketName, fileName, stream, "application/octet-stream"); return putFile(bucketName, fileName, stream, "application/octet-stream");
} }
/**
* 上传文件到MinIO
*
* @param bucketName 存储桶名称
* @param fileName 文件名
* @param stream 输入流
* @param contentType 文件类型
* @return BladeFile 上传文件信息
*/
@SneakyThrows @SneakyThrows
public BladeFile putFile(String bucketName, String fileName, InputStream stream, String contentType) { public BladeFile putFile(String bucketName, String fileName, InputStream stream, String contentType) {
makeBucket(bucketName); makeBucket(bucketName);
@ -251,41 +276,41 @@ public class MinioTemplate implements OssTemplate {
} }
/** /**
* 根据规则生成存储桶名称规则 * 获取默认存储桶名称
* *
* @return String * @return String 存储桶名称
*/ */
private String getBucketName() { private String getBucketName() {
return getBucketName(ossProperties.getBucketName()); return getBucketName(ossProperties.getBucketName());
} }
/** /**
* 根据规则生成存储桶名称规则 * 根据规则生成存储桶名称
* *
* @param bucketName 存储桶名称 * @param bucketName 存储桶名称
* @return String * @return String 处理后的存储桶名称
*/ */
private String getBucketName(String bucketName) { private String getBucketName(String bucketName) {
return ossRule.bucketName(bucketName); return ossRule.bucketName(bucketName);
} }
/** /**
* 根据规则生成文件名称规则 * 根据规则生成文件名称
* *
* @param originalFilename 原始文件名 * @param originalFilename 原始文件名
* @return string * @return String 处理后的文件名
*/ */
private String getFileName(String originalFilename) { private String getFileName(String originalFilename) {
return ossRule.fileName(originalFilename); return ossRule.fileName(originalFilename);
} }
/** /**
* 获取文件外链 * 获取预签名对象URL
* *
* @param bucketName bucket名称 * @param bucketName 存储桶名称
* @param fileName 文件名 * @param fileName 文件名
* @param expires 过期时间 * @param expires 过期时间
* @return url * @return String 预签名URL
*/ */
@SneakyThrows @SneakyThrows
public String getPresignedObjectUrl(String bucketName, String fileName, Integer expires) { public String getPresignedObjectUrl(String bucketName, String fileName, Integer expires) {
@ -300,21 +325,21 @@ public class MinioTemplate implements OssTemplate {
} }
/** /**
* 获取存储桶策略 * 获取存储桶策略配置
* *
* @param policyType 策略枚举 * @param policyType 策略类型
* @return String * @return String 存储桶策略
*/ */
public String getPolicyType(PolicyType policyType) { public String getPolicyType(PolicyType policyType) {
return getPolicyType(getBucketName(), policyType); return getPolicyType(getBucketName(), policyType);
} }
/** /**
* 获取存储桶策略 * 获取存储桶策略配置
* *
* @param bucketName 存储桶名称 * @param bucketName 存储桶名称
* @param policyType 策略枚举 * @param policyType 策略类型
* @return String * @return String 存储桶策略
*/ */
public static String getPolicyType(String bucketName, PolicyType policyType) { public static String getPolicyType(String bucketName, PolicyType policyType) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
@ -397,28 +422,28 @@ public class MinioTemplate implements OssTemplate {
} }
/** /**
* 获取域名 * 获取指定存储桶的访问域名
* *
* @param bucketName 存储桶名称 * @param bucketName 存储桶名称
* @return String * @return String 访问域名
*/ */
public String getOssHost(String bucketName) { public String getOssHost(String bucketName) {
return getEndpoint() + StringPool.SLASH + getBucketName(bucketName); return getEndpoint() + StringPool.SLASH + getBucketName(bucketName);
} }
/** /**
* 获取域名 * 获取默认存储桶的访问域名
* *
* @return String * @return String 访问域名
*/ */
public String getOssHost() { public String getOssHost() {
return getOssHost(ossProperties.getBucketName()); return getOssHost(ossProperties.getBucketName());
} }
/** /**
* 获取服务地址 * 获取MinIO服务的Endpoint
* *
* @return String * @return String MinIO Endpoint
*/ */
public String getEndpoint() { public String getEndpoint() {
if (StringUtil.isBlank(ossProperties.getTransformEndpoint())) { if (StringUtil.isBlank(ossProperties.getTransformEndpoint())) {

View File

@ -44,10 +44,29 @@ import java.util.List;
*/ */
@AllArgsConstructor @AllArgsConstructor
public class QiniuTemplate implements OssTemplate { public class QiniuTemplate implements OssTemplate {
/**
* 七牛云认证对象
*/
private final Auth auth; private final Auth auth;
/**
* 七牛云上传管理对象
*/
private final UploadManager uploadManager; private final UploadManager uploadManager;
/**
* 七牛云存储空间管理对象
*/
private final BucketManager bucketManager; private final BucketManager bucketManager;
/**
* OSS配置属性
*/
private final OssProperties ossProperties; private final OssProperties ossProperties;
/**
* OSS规则对象
*/
private final OssRule ossRule; private final OssRule ossRule;
@Override @Override
@ -156,6 +175,15 @@ public class QiniuTemplate implements OssTemplate {
return put(bucketName, stream, fileName, false); return put(bucketName, stream, fileName, false);
} }
/**
* 上传文件到七牛云
*
* @param bucketName 存储空间名称
* @param stream 输入流
* @param key 文件名
* @param cover 是否覆盖上传
* @return BladeFile 上传文件信息
*/
@SneakyThrows @SneakyThrows
public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) { public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) {
makeBucket(bucketName); makeBucket(bucketName);
@ -206,68 +234,68 @@ public class QiniuTemplate implements OssTemplate {
} }
/** /**
* 根据规则生成存储桶名称规则 * 获取默认存储空间名称
* *
* @return String * @return String 存储空间名称
*/ */
private String getBucketName() { private String getBucketName() {
return getBucketName(ossProperties.getBucketName()); return getBucketName(ossProperties.getBucketName());
} }
/** /**
* 根据规则生成存储桶名称规则 * 根据规则生成存储空间名称
* *
* @param bucketName 存储名称 * @param bucketName 存储空间名称
* @return String * @return String 处理后的存储空间名称
*/ */
private String getBucketName(String bucketName) { private String getBucketName(String bucketName) {
return ossRule.bucketName(bucketName); return ossRule.bucketName(bucketName);
} }
/** /**
* 根据规则生成文件名称规则 * 根据规则生成文件名称
* *
* @param originalFilename 原始文件名 * @param originalFilename 原始文件名
* @return string * @return String 处理后的文件名
*/ */
private String getFileName(String originalFilename) { private String getFileName(String originalFilename) {
return ossRule.fileName(originalFilename); return ossRule.fileName(originalFilename);
} }
/** /**
* 获取上传凭证普通上传 * 获取普通上传凭证
* *
* @param bucketName 存储名称 * @param bucketName 存储空间名称
* @return string * @return String 上传凭证
*/ */
public String getUploadToken(String bucketName) { public String getUploadToken(String bucketName) {
return auth.uploadToken(getBucketName(bucketName)); return auth.uploadToken(getBucketName(bucketName));
} }
/** /**
* 获取上传凭证覆盖上传 * 获取覆盖上传凭证
* *
* @param bucketName 存储名称 * @param bucketName 存储空间名称
* @param key key * @param key 文件名
* @return string * @return String 上传凭证
*/ */
private String getUploadToken(String bucketName, String key) { private String getUploadToken(String bucketName, String key) {
return auth.uploadToken(getBucketName(bucketName), key); return auth.uploadToken(getBucketName(bucketName), key);
} }
/** /**
* 获取域名 * 获取七牛云访问域名
* *
* @return String * @return String 访问域名
*/ */
public String getOssHost() { public String getOssHost() {
return getEndpoint(); return getEndpoint();
} }
/** /**
* 获取服务地址 * 获取七牛云服务的Endpoint
* *
* @return String * @return String 七牛云Endpoint
*/ */
public String getEndpoint() { public String getEndpoint() {
if (StringUtil.isBlank(ossProperties.getTransformEndpoint())) { if (StringUtil.isBlank(ossProperties.getTransformEndpoint())) {
@ -276,5 +304,4 @@ public class QiniuTemplate implements OssTemplate {
return ossProperties.getTransformEndpoint(); return ossProperties.getTransformEndpoint();
} }
} }

View File

@ -39,12 +39,22 @@ import java.util.List;
* </p> * </p>
* *
* @author yangkai.shen * @author yangkai.shen
* @date Created in 2020/1/7 17:24
*/ */
@AllArgsConstructor @AllArgsConstructor
public class TencentCosTemplate implements OssTemplate { public class TencentCosTemplate implements OssTemplate {
/**
* 腾讯云 COS 客户端
*/
private final COSClient cosClient; private final COSClient cosClient;
/**
* OSS配置属性
*/
private final OssProperties ossProperties; private final OssProperties ossProperties;
/**
* OSS规则对象
*/
private final OssRule ossRule; private final OssRule ossRule;
@Override @Override
@ -166,6 +176,15 @@ public class TencentCosTemplate implements OssTemplate {
return put(bucketName, stream, fileName, false); return put(bucketName, stream, fileName, false);
} }
/**
* 上传文件到腾讯云COS
*
* @param bucketName 存储桶名称
* @param stream 输入流
* @param key 文件名
* @param cover 是否覆盖上传
* @return BladeFile 上传文件信息
*/
@SneakyThrows @SneakyThrows
public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) { public BladeFile put(String bucketName, InputStream stream, String key, boolean cover) {
makeBucket(bucketName); makeBucket(bucketName);
@ -217,39 +236,39 @@ public class TencentCosTemplate implements OssTemplate {
} }
/** /**
* 根据规则生成存储桶名称规则 * 获取默认存储桶名称
* *
* @return String * @return String 存储桶名称
*/ */
private String getBucketName() { private String getBucketName() {
return getBucketName(ossProperties.getBucketName()); return getBucketName(ossProperties.getBucketName());
} }
/** /**
* 根据规则生成存储桶名称规则 * 根据规则生成存储桶名称
* *
* @param bucketName 存储桶名称 * @param bucketName 存储桶名称
* @return String * @return String 处理后的存储桶名称
*/ */
private String getBucketName(String bucketName) { private String getBucketName(String bucketName) {
return ossRule.bucketName(bucketName).concat(StringPool.DASH).concat(ossProperties.getAppId()); return ossRule.bucketName(bucketName).concat(StringPool.DASH).concat(ossProperties.getAppId());
} }
/** /**
* 根据规则生成文件名称规则 * 根据规则生成文件名称
* *
* @param originalFilename 原始文件名 * @param originalFilename 原始文件名
* @return string * @return String 处理后的文件名
*/ */
private String getFileName(String originalFilename) { private String getFileName(String originalFilename) {
return ossRule.fileName(originalFilename); return ossRule.fileName(originalFilename);
} }
/** /**
* 获取域名 * 获取指定存储桶的访问域名
* *
* @param bucketName 存储桶名称 * @param bucketName 存储桶名称
* @return String * @return String 访问域名
*/ */
public String getOssHost(String bucketName) { public String getOssHost(String bucketName) {
String prefix = getEndpoint().contains("https://") ? "https://" : "http://"; String prefix = getEndpoint().contains("https://") ? "https://" : "http://";
@ -257,18 +276,18 @@ public class TencentCosTemplate implements OssTemplate {
} }
/** /**
* 获取域名 * 获取默认存储桶的访问域名
* *
* @return String * @return String 访问域名
*/ */
public String getOssHost() { public String getOssHost() {
return getOssHost(ossProperties.getBucketName()); return getOssHost(ossProperties.getBucketName());
} }
/** /**
* 获取服务地址 * 获取腾讯云COS服务的Endpoint
* *
* @return String * @return String 腾讯云COS Endpoint
*/ */
public String getEndpoint() { public String getEndpoint() {
if (StringUtil.isBlank(ossProperties.getTransformEndpoint())) { if (StringUtil.isBlank(ossProperties.getTransformEndpoint())) {

View File

@ -15,10 +15,6 @@
*/ */
package org.springblade.core.oss.config; package org.springblade.core.oss.config;
import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import org.springblade.core.oss.AliossTemplate; import org.springblade.core.oss.AliossTemplate;
import org.springblade.core.oss.props.OssProperties; import org.springblade.core.oss.props.OssProperties;
import org.springblade.core.oss.rule.OssRule; import org.springblade.core.oss.rule.OssRule;
@ -30,8 +26,15 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
/** /**
* Alioss配置类 * 阿里云OSS对象存储配置类
* 用于配置阿里云OSS客户端及其模板类
* 仅在配置文件中指定 oss.name=alioss 时生效
* *
* @author Chill * @author Chill
*/ */
@ -41,6 +44,20 @@ import org.springframework.context.annotation.Bean;
@ConditionalOnProperty(value = "oss.name", havingValue = "alioss") @ConditionalOnProperty(value = "oss.name", havingValue = "alioss")
public class AliossConfiguration { public class AliossConfiguration {
/**
* 配置阿里云OSS客户端
* 当容器中不存在 OSSClient 类型的Bean时生效
* 配置包括
* - 最大HTTP连接数
* - Socket传输超时时间
* - 建立连接超时时间
* - 连接池获取连接超时时间
* - 连接空闲超时时间
* - 请求失败重试次数
*
* @param ossProperties OSS配置属性
* @return OSSClient 阿里云OSS客户端实例
*/
@Bean @Bean
@ConditionalOnMissingBean(OSSClient.class) @ConditionalOnMissingBean(OSSClient.class)
public OSSClient ossClient(OssProperties ossProperties) { public OSSClient ossClient(OssProperties ossProperties) {
@ -62,6 +79,15 @@ public class AliossConfiguration {
return new OSSClient(ossProperties.getEndpoint(), credentialsProvider, conf); return new OSSClient(ossProperties.getEndpoint(), credentialsProvider, conf);
} }
/**
* 配置阿里云OSS操作模板
* 需要容器中存在 OSSClient 的Bean且不存在 AliossTemplate 的Bean时生效
*
* @param ossRule OSS规则配置
* @param ossProperties OSS配置属性
* @param ossClient 阿里云OSS客户端
* @return AliossTemplate 阿里云OSS操作模板
*/
@Bean @Bean
@ConditionalOnBean({OSSClient.class}) @ConditionalOnBean({OSSClient.class})
@ConditionalOnMissingBean(AliossTemplate.class) @ConditionalOnMissingBean(AliossTemplate.class)

View File

@ -15,8 +15,6 @@
*/ */
package org.springblade.core.oss.config; package org.springblade.core.oss.config;
import io.minio.MinioClient;
import lombok.SneakyThrows;
import org.springblade.core.oss.MinioTemplate; import org.springblade.core.oss.MinioTemplate;
import org.springblade.core.oss.props.OssProperties; import org.springblade.core.oss.props.OssProperties;
import org.springblade.core.oss.rule.OssRule; import org.springblade.core.oss.rule.OssRule;
@ -28,8 +26,13 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import io.minio.MinioClient;
import lombok.SneakyThrows;
/** /**
* Minio配置类 * MinIO对象存储配置类
* 用于配置MinIO客户端及其模板类
* 仅在配置文件中指定 oss.name=minio 时生效
* *
* @author Chill * @author Chill
*/ */
@ -39,6 +42,13 @@ import org.springframework.context.annotation.Bean;
@ConditionalOnProperty(value = "oss.name", havingValue = "minio") @ConditionalOnProperty(value = "oss.name", havingValue = "minio")
public class MinioConfiguration { public class MinioConfiguration {
/**
* 配置MinIO客户端
* 当容器中不存在 MinioClient 类型的Bean时生效
*
* @param ossProperties OSS配置属性
* @return MinioClient MinIO客户端实例
*/
@Bean @Bean
@SneakyThrows @SneakyThrows
@ConditionalOnMissingBean(MinioClient.class) @ConditionalOnMissingBean(MinioClient.class)
@ -49,6 +59,15 @@ public class MinioConfiguration {
.build(); .build();
} }
/**
* 配置MinIO操作模板
* 需要容器中存在 MinioClient 的Bean且不存在 MinioTemplate 的Bean时生效
*
* @param ossRule OSS规则配置
* @param ossProperties OSS配置属性
* @param minioClient MinIO客户端
* @return MinioTemplate MinIO操作模板
*/
@Bean @Bean
@ConditionalOnBean({MinioClient.class}) @ConditionalOnBean({MinioClient.class})
@ConditionalOnMissingBean(MinioTemplate.class) @ConditionalOnMissingBean(MinioTemplate.class)

View File

@ -24,7 +24,8 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
/** /**
* Oss配置类 * OSS对象存储配置类
* 用于配置通用的OSS规则和属性
* *
* @author Chill * @author Chill
*/ */
@ -32,6 +33,12 @@ import org.springframework.context.annotation.Bean;
@EnableConfigurationProperties(OssProperties.class) @EnableConfigurationProperties(OssProperties.class)
public class OssConfiguration { public class OssConfiguration {
/**
* 配置默认的OSS规则实现
* 当容器中不存在 OssRule 类型的Bean时生效
*
* @return OssRule OSS规则实现类
*/
@Bean @Bean
@ConditionalOnMissingBean(OssRule.class) @ConditionalOnMissingBean(OssRule.class)
public OssRule ossRule() { public OssRule ossRule() {

View File

@ -15,10 +15,6 @@
*/ */
package org.springblade.core.oss.config; package org.springblade.core.oss.config;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springblade.core.oss.QiniuTemplate; import org.springblade.core.oss.QiniuTemplate;
import org.springblade.core.oss.props.OssProperties; import org.springblade.core.oss.props.OssProperties;
import org.springblade.core.oss.rule.OssRule; import org.springblade.core.oss.rule.OssRule;
@ -30,8 +26,15 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
/** /**
* Qiniu配置类 * 七牛云对象存储配置类
* 用于配置七牛云存储的客户端组件及其模板类
* 仅在配置文件中指定 oss.name=qiniu 时生效
* *
* @author Chill * @author Chill
*/ */
@ -41,24 +44,53 @@ import org.springframework.context.annotation.Bean;
@ConditionalOnProperty(value = "oss.name", havingValue = "qiniu") @ConditionalOnProperty(value = "oss.name", havingValue = "qiniu")
public class QiniuConfiguration { public class QiniuConfiguration {
/**
* 配置七牛云存储配置对象
* 当容器中不存在 Configuration 类型的Bean时生效
* 使用自动区域配置
*
* @return Configuration 七牛云存储配置对象
*/
@Bean @Bean
@ConditionalOnMissingBean(com.qiniu.storage.Configuration.class) @ConditionalOnMissingBean(com.qiniu.storage.Configuration.class)
public com.qiniu.storage.Configuration qnConfiguration() { public com.qiniu.storage.Configuration qnConfiguration() {
return new com.qiniu.storage.Configuration(Region.autoRegion()); return new com.qiniu.storage.Configuration(Region.autoRegion());
} }
/**
* 配置七牛云认证对象
* 当容器中不存在 Auth 类型的Bean时生效
*
* @param ossProperties OSS配置属性
* @return Auth 七牛云认证对象
*/
@Bean @Bean
@ConditionalOnMissingBean(Auth.class) @ConditionalOnMissingBean(Auth.class)
public Auth auth(OssProperties ossProperties) { public Auth auth(OssProperties ossProperties) {
return Auth.create(ossProperties.getAccessKey(), ossProperties.getSecretKey()); return Auth.create(ossProperties.getAccessKey(), ossProperties.getSecretKey());
} }
/**
* 配置七牛云上传管理器
* 需要容器中存在 Configuration 类型的Bean时生效
*
* @param cfg 七牛云存储配置对象
* @return UploadManager 七牛云上传管理器
*/
@Bean @Bean
@ConditionalOnBean(com.qiniu.storage.Configuration.class) @ConditionalOnBean(com.qiniu.storage.Configuration.class)
public UploadManager uploadManager(com.qiniu.storage.Configuration cfg) { public UploadManager uploadManager(com.qiniu.storage.Configuration cfg) {
return new UploadManager(cfg); return new UploadManager(cfg);
} }
/**
* 配置七牛云存储空间管理器
* 需要容器中存在 Configuration 类型的Bean时生效
*
* @param ossProperties OSS配置属性
* @param cfg 七牛云存储配置对象
* @return BucketManager 七牛云存储空间管理器
*/
@Bean @Bean
@ConditionalOnBean(com.qiniu.storage.Configuration.class) @ConditionalOnBean(com.qiniu.storage.Configuration.class)
public BucketManager bucketManager(OssProperties ossProperties, public BucketManager bucketManager(OssProperties ossProperties,
@ -66,6 +98,17 @@ public class QiniuConfiguration {
return new BucketManager(Auth.create(ossProperties.getAccessKey(), ossProperties.getSecretKey()), cfg); return new BucketManager(Auth.create(ossProperties.getAccessKey(), ossProperties.getSecretKey()), cfg);
} }
/**
* 配置七牛云存储操作模板
* 需要容器中存在 AuthUploadManagerBucketManager 的Bean且不存在 QiniuTemplate 的Bean时生效
*
* @param ossRule OSS规则配置
* @param ossProperties OSS配置属性
* @param auth 七牛云认证对象
* @param uploadManager 七牛云上传管理器
* @param bucketManager 七牛云存储空间管理器
* @return QiniuTemplate 七牛云存储操作模板
*/
@Bean @Bean
@ConditionalOnBean({Auth.class, UploadManager.class, BucketManager.class}) @ConditionalOnBean({Auth.class, UploadManager.class, BucketManager.class})
@ConditionalOnMissingBean(QiniuTemplate.class) @ConditionalOnMissingBean(QiniuTemplate.class)

View File

@ -15,12 +15,6 @@
*/ */
package org.springblade.core.oss.config; package org.springblade.core.oss.config;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import lombok.AllArgsConstructor;
import org.springblade.core.oss.TencentCosTemplate; import org.springblade.core.oss.TencentCosTemplate;
import org.springblade.core.oss.props.OssProperties; import org.springblade.core.oss.props.OssProperties;
import org.springblade.core.oss.rule.OssRule; import org.springblade.core.oss.rule.OssRule;
@ -32,13 +26,20 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import lombok.AllArgsConstructor;
/** /**
* <p> * <p>
* 腾讯云 COS 自动装配 * 腾讯云 COS 自动装配
* </p> * </p>
* *
* @author yangkai.shen * @author yangkai.shen
* @date Created in 2020/1/7 17:24
*/ */
@AllArgsConstructor @AllArgsConstructor
@AutoConfiguration(after = OssConfiguration.class) @AutoConfiguration(after = OssConfiguration.class)
@ -47,10 +48,29 @@ import org.springframework.context.annotation.Bean;
@ConditionalOnProperty(value = "oss.name", havingValue = "tencentcos") @ConditionalOnProperty(value = "oss.name", havingValue = "tencentcos")
public class TencentCosConfiguration { public class TencentCosConfiguration {
/**
* OSS配置属性
*/
private final OssProperties ossProperties; private final OssProperties ossProperties;
/**
* OSS规则对象
*/
private final OssRule ossRule; private final OssRule ossRule;
/**
* 配置腾讯云COS客户端
* 当容器中不存在 COSClient 类型的Bean时生效
* 配置包括
* - 用户身份信息secretId, secretKey
* - 存储桶区域设置
* - 最大HTTP连接数
* - Socket传输超时时间
* - 建立连接超时时间
* - 连接池获取连接超时时间
*
* @return COSClient 腾讯云COS客户端实例
*/
@Bean @Bean
@ConditionalOnMissingBean(COSClient.class) @ConditionalOnMissingBean(COSClient.class)
public COSClient ossClient() { public COSClient ossClient() {
@ -71,6 +91,13 @@ public class TencentCosConfiguration {
return new COSClient(credentials, clientConfig); return new COSClient(credentials, clientConfig);
} }
/**
* 配置腾讯云COS操作模板
* 需要容器中存在 COSClient 的Bean且不存在 TencentCosTemplate 的Bean时生效
*
* @param cosClient 腾讯云COS客户端
* @return TencentCosTemplate 腾讯云COS操作模板
*/
@Bean @Bean
@ConditionalOnBean({COSClient.class}) @ConditionalOnBean({COSClient.class})
@ConditionalOnMissingBean(TencentCosTemplate.class) @ConditionalOnMissingBean(TencentCosTemplate.class)

View File

@ -690,9 +690,6 @@
</goals> </goals>
</execution> </execution>
</executions> </executions>
<configuration>
<failOnError>false</failOnError>
</configuration>
</plugin> </plugin>
<!-- GPG --> <!-- GPG -->
<plugin> <plugin>