feat[文件上传]:oss阿里云对象存储集成
This commit is contained in:
@@ -80,9 +80,28 @@
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
</dependency>-->
|
||||
<!-- aws s3 -->
|
||||
<dependency>
|
||||
<!-- <dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-s3</artifactId>
|
||||
</dependency>-->
|
||||
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.xml.bind</groupId>
|
||||
<artifactId>jaxb-api</artifactId>
|
||||
<version>2.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.activation</groupId>
|
||||
<artifactId>activation</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
<!-- no more than 2.3.3-->
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>jaxb-runtime</artifactId>
|
||||
<version>2.3.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.central.file.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.file.model.FileInfo;
|
||||
import com.central.oss.model.ObjectInfo;
|
||||
import com.central.oss.properties.FileServerProperties;
|
||||
import com.central.oss.template.OssTemplate;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* @author lsl
|
||||
* @date 2025/1/13
|
||||
* <p>
|
||||
*/
|
||||
@Service
|
||||
@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_OSS)
|
||||
public class OssService extends AbstractIFileService {
|
||||
|
||||
@Resource
|
||||
private OssTemplate ossTemplate;
|
||||
|
||||
@Override
|
||||
protected String fileType() {
|
||||
return FileServerProperties.TYPE_COS;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectInfo uploadFile(MultipartFile file) {
|
||||
return ossTemplate.upload(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectInfo uploadFile(String name, InputStream inputStream) throws IOException {
|
||||
return ossTemplate.upload(name, inputStream,inputStream.available(),"image/jpeg");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deleteFile(String objectPath) {
|
||||
OSSObject ossObject = parsePath(objectPath);
|
||||
ossTemplate.delete(ossObject.getBucketName(), ossObject.getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void out(String id, OutputStream os) {
|
||||
FileInfo fileInfo = baseMapper.selectById(id);
|
||||
if (fileInfo != null) {
|
||||
OSSObject ossObject = parsePath(fileInfo.getPath());
|
||||
ossTemplate.out(ossObject.getBucketName(), ossObject.getKey(), os);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getViewUrl(String fileId) {
|
||||
FileInfo fileInfo = baseMapper.selectById(fileId);
|
||||
if (fileInfo != null) {
|
||||
return ossTemplate.getViewUrl(fileInfo.getName());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo getFileInfo(String id) {
|
||||
return baseMapper.selectById(id);
|
||||
}
|
||||
|
||||
private OSSObject parsePath(String path) {
|
||||
OSSObject ossObject = new OSSObject();
|
||||
if (StrUtil.isNotEmpty(path)) {
|
||||
int splitIndex = path.lastIndexOf(CommonConstant.PATH_SPLIT);
|
||||
if (splitIndex != -1) {
|
||||
ossObject.setBucketName(path.substring(0, splitIndex));
|
||||
ossObject.setKey(path.substring(splitIndex + 1));
|
||||
}
|
||||
}
|
||||
return ossObject;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,7 +26,7 @@ knife4j:
|
||||
|
||||
zlt:
|
||||
file-server:
|
||||
type: cos
|
||||
type: oss
|
||||
#fdfs:
|
||||
# web-url: ${zlt.fdfs.web-url}
|
||||
s3:
|
||||
@@ -40,6 +40,11 @@ zlt:
|
||||
endpoint: ${zlt.cos.endpoint}
|
||||
region: ${zlt.cos.region}
|
||||
bucketName: ${zlt.cos.bucketName}
|
||||
oss:
|
||||
secretId: ${zlt.oss.secretId}
|
||||
secretKey: ${zlt.oss.secretKey}
|
||||
endpoint: ${zlt.oss.endpoint}
|
||||
bucketName: ${zlt.oss.bucketName}
|
||||
#多租户配置
|
||||
tenant:
|
||||
enable: true
|
||||
|
||||
@@ -4,7 +4,7 @@ server:
|
||||
spring:
|
||||
application:
|
||||
name: file-center
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: 119.45.169.13
|
||||
# cloud:
|
||||
# nacos:
|
||||
# discovery:
|
||||
# ip: 119.45.169.13
|
||||
|
||||
+3
-3
@@ -150,6 +150,9 @@ public class OrderController {
|
||||
|
||||
LoginAppUser user = LoginUserContextHolder.getUser();
|
||||
order.setUserId(user.getId());
|
||||
if (order.getCodeId() == null){
|
||||
order.setCodeId("f83be3a9-2888-4000-8cf9-e6acdb3a22cd");
|
||||
}
|
||||
QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId());
|
||||
if (qrCode != null){
|
||||
order.setTourismId(qrCode.getTourismId());
|
||||
@@ -174,9 +177,6 @@ public class OrderController {
|
||||
public Result<PrepayWithRequestPaymentResponse> payOrder(@PathVariable("orderId") Long orderId) {
|
||||
|
||||
SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId());
|
||||
if (user == null){
|
||||
return Result.failed("登录信息失效");
|
||||
}
|
||||
Order order = orderService.getById(orderId);
|
||||
if (order == null) {
|
||||
return Result.failed("订单数据不存在");
|
||||
|
||||
@@ -35,6 +35,7 @@ public class Order extends SuperEntity {
|
||||
private Integer orderStatus;
|
||||
|
||||
private Date payTime;
|
||||
@Transient
|
||||
private String createdTime;
|
||||
@Transient
|
||||
private String codeId;
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
|
||||
<mapper namespace="com.central.order.mapper.OrderFileMapper">
|
||||
<select id="findList" resultType="com.central.order.model.OrderFile">
|
||||
select * from order_files t
|
||||
select * from order_files t ORDER BY id DESC
|
||||
</select>
|
||||
|
||||
<!-- 动态获取参数采用备参 -->
|
||||
<select id="findListByUser" resultType="com.central.order.model.OrderFile">
|
||||
select * from order_files t WHERE t.user_id = #{param2.userId}
|
||||
select * from order_files t WHERE t.user_id = #{param2.userId} ORDER BY id DESC
|
||||
</select>
|
||||
|
||||
<insert id="insertOrderFile">
|
||||
|
||||
@@ -3,17 +3,17 @@
|
||||
|
||||
<mapper namespace="com.central.order.mapper.OrderMapper">
|
||||
<select id="findList" resultType="com.central.order.model.Order">
|
||||
select * from orders t
|
||||
select * from orders t ORDER BY id DESC
|
||||
</select>
|
||||
|
||||
<!-- 动态获取参数采用备参 -->
|
||||
<select id="findListByUser" resultType="com.central.order.model.Order">
|
||||
select id,order_no,tourism_id,device_sn,workspace_id,code_id,user_id,order_amount,order_status,create_time,update_time from orders t WHERE t.user_id = #{param2.userId}
|
||||
select id,order_no,tourism_id,device_sn,workspace_id,code_id,user_id,order_amount,order_status,create_time,update_time from orders t WHERE t.user_id = #{param2.userId} ORDER BY id DESC
|
||||
</select>
|
||||
|
||||
<!-- 根据订单状态查询订单列表 -->
|
||||
<select id="getOrdersByStatus" resultMap="OrderResultMap">
|
||||
SELECT * FROM orders WHERE order_status = #{status}
|
||||
SELECT * FROM orders WHERE order_status = #{status} ORDER BY id DESC
|
||||
</select>
|
||||
<!-- 新增订单 -->
|
||||
<insert id="insertOrder" keyProperty="order_id" useGeneratedKeys="true">
|
||||
|
||||
@@ -41,5 +41,11 @@
|
||||
<version>5.6.227</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>3.17.4</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ 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.OssTemplate;
|
||||
import com.central.oss.template.S3Template;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Import;
|
||||
@@ -13,7 +14,7 @@ import org.springframework.context.annotation.Import;
|
||||
* <p>
|
||||
*/
|
||||
@EnableConfigurationProperties(FileServerProperties.class)
|
||||
@Import({FdfsTemplate.class, S3Template.class, CosTemplate.class})
|
||||
@Import({FdfsTemplate.class, S3Template.class, CosTemplate.class, OssTemplate.class})
|
||||
public class OssAutoConfigure {
|
||||
|
||||
}
|
||||
|
||||
+6
@@ -19,6 +19,7 @@ public class FileServerProperties {
|
||||
public static final String TYPE_FDFS = "fastdfs";
|
||||
public static final String TYPE_S3 = "s3";
|
||||
public static final String TYPE_COS = "cos";
|
||||
public static final String TYPE_OSS = "oss";
|
||||
|
||||
/**
|
||||
* 为以下2个值,指定不同的自动化配置
|
||||
@@ -41,4 +42,9 @@ public class FileServerProperties {
|
||||
* cos 配置
|
||||
*/
|
||||
CosProperties cos = new CosProperties();
|
||||
|
||||
/**
|
||||
* cos 配置
|
||||
*/
|
||||
OssProperties oss = new OssProperties();
|
||||
}
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.central.oss.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* oss 协议配置
|
||||
*
|
||||
* @author lsl
|
||||
* @date 2025/1/13
|
||||
* <p>
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class OssProperties {
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String secretId;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String secretKey;
|
||||
/**
|
||||
* bucket名称
|
||||
*/
|
||||
private String bucketName;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private String endpoint;
|
||||
/**
|
||||
* 区域
|
||||
*/
|
||||
private String region;
|
||||
/**
|
||||
* path-style
|
||||
*/
|
||||
private Boolean pathStyleAccessEnabled = true;
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
package com.central.oss.template;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.central.oss.model.ObjectInfo;
|
||||
import com.central.oss.properties.FileServerProperties;
|
||||
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(OSS.class)
|
||||
@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_OSS)
|
||||
public class OssTemplate implements InitializingBean {
|
||||
|
||||
private static final String DEF_CONTEXT_TYPE = "application/octet-stream";
|
||||
private static final String PATH_SPLIT = "/";
|
||||
|
||||
@Autowired
|
||||
private FileServerProperties fileProperties;
|
||||
|
||||
private OSS ossClient;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
|
||||
// 生成 oss 客户端
|
||||
this.ossClient = new OSSClientBuilder()
|
||||
.build(fileProperties.getOss().getEndpoint(), fileProperties.getOss().getSecretId(), fileProperties.getOss().getSecretKey());
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public ObjectInfo upload(String fileName, InputStream is) {
|
||||
return upload(fileProperties.getOss().getBucketName(), fileName, is, is.available(), DEF_CONTEXT_TYPE);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public ObjectInfo upload(MultipartFile file) {
|
||||
return upload(fileProperties.getOss().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 对象名
|
||||
*/
|
||||
public 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);
|
||||
|
||||
ossClient.putObject(putObjectRequest);
|
||||
|
||||
ObjectInfo obj = new ObjectInfo();
|
||||
obj.setObjectPath(bucketName + PATH_SPLIT + objectName);
|
||||
obj.setObjectUrl(fileProperties.getCos().getEndpoint() + PATH_SPLIT + obj.getObjectPath());
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传对象
|
||||
* @param objectName 对象名
|
||||
*/
|
||||
public ObjectInfo upload(String objectName, InputStream is, int size, String contentType) {
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
objectMetadata.setContentLength(size);
|
||||
objectMetadata.setContentType(contentType);
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(fileProperties.getCos().getBucketName(), objectName, is, objectMetadata);
|
||||
|
||||
ossClient.putObject(putObjectRequest);
|
||||
|
||||
ObjectInfo obj = new ObjectInfo();
|
||||
obj.setObjectPath(fileProperties.getCos().getBucketName() + 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) {
|
||||
ossClient.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 = ossClient.generatePresignedUrl(bucketName, objectName, cal.getTime());
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预览地址
|
||||
* @param objectName 对象名
|
||||
* @param expires 有效时间(分钟),最大7天有效
|
||||
* @return
|
||||
*/
|
||||
public String getViewUrl(String objectName, int expires) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(Calendar.MINUTE, expires);
|
||||
URL url = ossClient.generatePresignedUrl(fileProperties.getCos().getBucketName(), objectName, cal.getTime());
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取预览地址
|
||||
* @param objectName 对象名
|
||||
* @return
|
||||
*/
|
||||
public String getViewUrl(String objectName) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(Calendar.MINUTE, 7);
|
||||
URL url = ossClient.generatePresignedUrl(fileProperties.getCos().getBucketName(), 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) {
|
||||
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
|
||||
try (
|
||||
InputStream ossis = ossObject.getObjectContent();
|
||||
) {
|
||||
// IOUtils.copy(ossis, os);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,11 @@ zlt.s3.endpoint=http://81.70.164.178:9000
|
||||
zlt.s3.bucketName=gofly
|
||||
zlt.s3.region=us-east-1
|
||||
|
||||
zlt.oss.secretId=LTAI5tSNV1RvexXpMMCZhxSe
|
||||
zlt.oss.secretKey=XocJRWU8emwjjvYLTrc04l91VZMkQK
|
||||
zlt.oss.endpoint=https://oss-cn-beijing.aliyuncs.com
|
||||
zlt.oss.bucketName=goflyapp
|
||||
|
||||
#### cos配置
|
||||
zlt.cos.secretId=AKIDkMn6B3h7KJ5dIkyzpRr7C6mFUqFWbu1u
|
||||
zlt.cos.secretKey=jDThssfEZieS4S8bqsgIiAUdbXVVYgdQ
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
########################## bootstrap级别通用配置 ##########################
|
||||
# 默认开发环境
|
||||
spring.profiles.active=prod
|
||||
spring.profiles.active=dev
|
||||
|
||||
##### nacos(注册中心和配置中心)地址
|
||||
spring.cloud.nacos.server-addr=47.94.136.130:8848
|
||||
#spring.cloud.nacos.server-addr=127.0.0.1:8848
|
||||
#spring.cloud.nacos.server-addr=47.94.136.130:8848
|
||||
spring.cloud.nacos.server-addr=127.0.0.1:8848
|
||||
spring.cloud.nacos.username=nacos
|
||||
spring.cloud.nacos.password=nacos
|
||||
spring.cloud.nacos.config.file-extension=yml
|
||||
|
||||
Reference in New Issue
Block a user