feat[文件上传和订单模块功能]:已测试通过
This commit is contained in:
@@ -34,5 +34,12 @@
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.qcloud</groupId>
|
||||
<artifactId>cos_api</artifactId>
|
||||
<version>5.6.227</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
+4
-5
@@ -1,20 +1,19 @@
|
||||
package com.central.oss.config;
|
||||
|
||||
import com.central.oss.properties.FileServerProperties;
|
||||
import com.central.oss.template.CosTemplate;
|
||||
import com.central.oss.template.FdfsTemplate;
|
||||
import com.central.oss.template.S3Template;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2021/2/13
|
||||
* @author lsl
|
||||
* @date 2025/1/13
|
||||
* <p>
|
||||
* Blog: http://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@EnableConfigurationProperties(FileServerProperties.class)
|
||||
@Import({FdfsTemplate.class, S3Template.class})
|
||||
@Import({FdfsTemplate.class, S3Template.class, CosTemplate.class})
|
||||
public class OssAutoConfigure {
|
||||
|
||||
}
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.central.oss.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* cos 协议配置
|
||||
*
|
||||
* @author lsl
|
||||
* @date 2025/1/13
|
||||
* <p>
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class CosProperties {
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String secretId;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String secretKey;
|
||||
/**
|
||||
* bucket名称
|
||||
*/
|
||||
private String bucketName;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private String endpoint;
|
||||
/**
|
||||
* 区域
|
||||
*/
|
||||
private String region;
|
||||
/**
|
||||
* path-style
|
||||
*/
|
||||
private Boolean pathStyleAccessEnabled = true;
|
||||
}
|
||||
+6
@@ -18,6 +18,7 @@ public class FileServerProperties {
|
||||
public static final String PREFIX = "zlt.file-server";
|
||||
public static final String TYPE_FDFS = "fastdfs";
|
||||
public static final String TYPE_S3 = "s3";
|
||||
public static final String TYPE_COS = "cos";
|
||||
|
||||
/**
|
||||
* 为以下2个值,指定不同的自动化配置
|
||||
@@ -35,4 +36,9 @@ public class FileServerProperties {
|
||||
* fastDFS配置
|
||||
*/
|
||||
FdfsProperties fdfs = new FdfsProperties();
|
||||
|
||||
/**
|
||||
* cos 配置
|
||||
*/
|
||||
CosProperties cos = new CosProperties();
|
||||
}
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package com.central.oss.template;
|
||||
|
||||
import com.central.oss.model.ObjectInfo;
|
||||
import com.central.oss.properties.FileServerProperties;
|
||||
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.http.HttpProtocol;
|
||||
import com.qcloud.cos.model.COSObject;
|
||||
import com.qcloud.cos.model.COSObjectInputStream;
|
||||
import com.qcloud.cos.model.ObjectMetadata;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.qcloud.cos.region.Region;
|
||||
import com.qcloud.cos.utils.IOUtils;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Calendar;
|
||||
|
||||
/**
|
||||
* cos 配置
|
||||
*
|
||||
* @author lsl
|
||||
* @date 2025/1/13
|
||||
* <p>
|
||||
*/
|
||||
@ConditionalOnClass(COSClient.class)
|
||||
@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_COS)
|
||||
public class CosTemplate implements InitializingBean {
|
||||
|
||||
private static final String DEF_CONTEXT_TYPE = "application/octet-stream";
|
||||
private static final String PATH_SPLIT = "/";
|
||||
|
||||
@Autowired
|
||||
private FileServerProperties fileProperties;
|
||||
|
||||
private COSClient cosClient;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
|
||||
// 1. 初始化用户身份信息(secretId, secretKey)。
|
||||
COSCredentials cred = new BasicCOSCredentials(fileProperties.getCos().getSecretId(), fileProperties.getCos().getSecretKey());
|
||||
|
||||
// 2. 初始化地域配置
|
||||
Region region = new Region(fileProperties.getCos().getRegion());
|
||||
ClientConfig clientConfig = new ClientConfig(region);
|
||||
clientConfig.setHttpProtocol(HttpProtocol.https);
|
||||
|
||||
// 3. 生成 cos 客户端
|
||||
this.cosClient = new COSClient(cred, clientConfig);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public ObjectInfo upload(String fileName, InputStream is) {
|
||||
return upload(fileProperties.getCos().getBucketName(), fileName, is, is.available(), DEF_CONTEXT_TYPE);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public ObjectInfo upload(MultipartFile file) {
|
||||
return upload(fileProperties.getCos().getBucketName(), file.getOriginalFilename(), file.getInputStream()
|
||||
, ((Long)file.getSize()).intValue(), file.getContentType());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public ObjectInfo upload(String bucketName, String fileName, InputStream is) {
|
||||
return upload(bucketName, fileName, is, is.available(), DEF_CONTEXT_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传对象
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 对象名
|
||||
*/
|
||||
private ObjectInfo upload(String bucketName, String objectName, InputStream is, int size, String contentType) {
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
objectMetadata.setContentLength(size);
|
||||
objectMetadata.setContentType(contentType);
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, is, objectMetadata);
|
||||
// putObjectRequest.getRequestClientOptions().setReadLimit(size + 1);
|
||||
|
||||
cosClient.putObject(putObjectRequest);
|
||||
|
||||
ObjectInfo obj = new ObjectInfo();
|
||||
obj.setObjectPath(bucketName + PATH_SPLIT + objectName);
|
||||
obj.setObjectUrl(fileProperties.getCos().getEndpoint() + PATH_SPLIT + obj.getObjectPath());
|
||||
return obj;
|
||||
}
|
||||
|
||||
public void delete(String objectName) {
|
||||
delete(fileProperties.getCos().getBucketName(), objectName);
|
||||
}
|
||||
|
||||
public void delete(String bucketName, String objectName) {
|
||||
cosClient.deleteObject(bucketName, objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预览地址
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 对象名
|
||||
* @param expires 有效时间(分钟),最大7天有效
|
||||
* @return
|
||||
*/
|
||||
public String getViewUrl(String bucketName, String objectName, int expires) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(Calendar.MINUTE, expires);
|
||||
URL url = cosClient.generatePresignedUrl(bucketName, objectName, cal.getTime());
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
public void out(String objectName, OutputStream os) {
|
||||
out(fileProperties.getS3().getBucketName(), objectName, os);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出对象
|
||||
* @param bucketName bucket名称
|
||||
* @param objectName 对象名
|
||||
* @param os 输出流
|
||||
*/
|
||||
@SneakyThrows
|
||||
public void out(String bucketName, String objectName, OutputStream os) {
|
||||
COSObject cosObject = cosClient.getObject(bucketName, objectName);
|
||||
try (
|
||||
COSObjectInputStream cosis = cosObject.getObjectContent();
|
||||
) {
|
||||
IOUtils.copy(cosis, os);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user