+ */
+@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;
+ }
+
+}
diff --git a/zlt-business/file-center/src/main/resources/application.yml b/zlt-business/file-center/src/main/resources/application.yml
index 5ebf98a5..f582cad8 100644
--- a/zlt-business/file-center/src/main/resources/application.yml
+++ b/zlt-business/file-center/src/main/resources/application.yml
@@ -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
diff --git a/zlt-business/file-center/src/main/resources/bootstrap.yml b/zlt-business/file-center/src/main/resources/bootstrap.yml
index 520787bf..ae6f04e8 100644
--- a/zlt-business/file-center/src/main/resources/bootstrap.yml
+++ b/zlt-business/file-center/src/main/resources/bootstrap.yml
@@ -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
diff --git a/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java b/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java
index bd79a93d..96907a5c 100644
--- a/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java
+++ b/zlt-business/order-center/src/main/java/com/central/order/controller/OrderController.java
@@ -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
*/ @EnableConfigurationProperties(FileServerProperties.class) -@Import({FdfsTemplate.class, S3Template.class, CosTemplate.class}) +@Import({FdfsTemplate.class, S3Template.class, CosTemplate.class, OssTemplate.class}) public class OssAutoConfigure { } diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FileServerProperties.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FileServerProperties.java index 221d3631..5132db5c 100644 --- a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FileServerProperties.java +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/FileServerProperties.java @@ -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(); } diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/OssProperties.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/OssProperties.java new file mode 100644 index 00000000..f74fdeb8 --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/properties/OssProperties.java @@ -0,0 +1,41 @@ +package com.central.oss.properties; + +import lombok.Getter; +import lombok.Setter; + +/** + * oss 协议配置 + * + * @author lsl + * @date 2025/1/13 + *
+ */ +@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; +} diff --git a/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/OssTemplate.java b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/OssTemplate.java new file mode 100644 index 00000000..a398ca63 --- /dev/null +++ b/zlt-commons/zlt-oss-spring-boot-starter/src/main/java/com/central/oss/template/OssTemplate.java @@ -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 + *
+ */ +@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); + } + } +} diff --git a/zlt-config/src/main/resources/application-dev.properties b/zlt-config/src/main/resources/application-dev.properties index d450323b..cdb37d02 100644 --- a/zlt-config/src/main/resources/application-dev.properties +++ b/zlt-config/src/main/resources/application-dev.properties @@ -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 diff --git a/zlt-config/src/main/resources/bootstrap.properties b/zlt-config/src/main/resources/bootstrap.properties index 3589fb96..469972e8 100644 --- a/zlt-config/src/main/resources/bootstrap.properties +++ b/zlt-config/src/main/resources/bootstrap.properties @@ -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