From 18e1e88fd9aff9903e82aa30bac2666c2bc7846d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E9=87=91=E5=9F=8E=20=28GSean=29?= Date: Thu, 9 Dec 2021 10:55:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(starter):=20starter=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- design-demos/pom.xml | 1 + design-demos/spring-state-demo/pom.xml | 34 ++++++++ .../main/java/com/gsean/state/StateApp.java | 41 +++++++++ .../state/config/BizStateMachinePersist.java | 39 +++++++++ .../state/config/StateMachineConfig.java | 85 +++++++++++++++++++ .../gsean/state/enums/TurnstileEvents.java | 12 +++ .../gsean/state/enums/TurnstileStates.java | 12 +++ .../java/com/gsean/state/StateAppTest.java | 70 +++++++++++++++ pom.xml | 2 + .../oss-aliyun-spring-boot-starter/pom.xml | 37 ++++++++ .../com/gsean/starter/AliyunStarterApp.java | 34 ++++++++ .../starter/config/AliyunOSSAutoConfig.java | 40 +++++++++ .../starter/props/ALiYunCoreProperty.java | 13 +++ .../starter/props/AliYunOssProperty.java | 47 ++++++++++ .../starter/service/AliyunOssService.java | 49 +++++++++++ .../main/resources/META-INF/spring.factories | 1 + starter-demos/oss-spring-boot-starter/pom.xml | 49 +++++++++++ .../com/gsean/oss/config/AliyunOSSConfig.java | 31 +++++++ .../com/gsean/oss/config/OssAutoConfig.java | 39 +++++++++ .../gsean/oss/config/TencentOSSConfig.java | 28 ++++++ .../gsean/oss/constant/CommonConstant.java | 17 ++++ .../com/gsean/oss/props/OSSCoreProperty.java | 47 ++++++++++ .../com/gsean/oss/service/OssService.java | 53 ++++++++++++ .../service/impl/AliyunOssServiceImpl.java | 54 ++++++++++++ .../service/impl/TencentOssServiceImpl.java | 74 ++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + .../oss-tencent-spring-boot-starter/pom.xml | 37 ++++++++ .../java/com/gsean/starter/TencentApp.java | 38 +++++++++ .../starter/config/TencentAutoConfig.java | 28 ++++++ .../starter/props/TencentCoreProperty.java | 13 +++ .../starter/props/TencentOssProperty.java | 47 ++++++++++ .../starter/service/TencentOssService.java | 67 +++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + starter-demos/pom.xml | 26 ++++++ starter-demos/starter-sample/pom.xml | 44 ++++++++++ .../starter/sample/StarterSampleApp.java | 19 +++++ .../src/main/resources/application.yml | 29 +++++++ .../starter/sample/StarterSampleAppTest.java | 70 +++++++++++++++ 38 files changed, 1329 insertions(+) create mode 100644 design-demos/spring-state-demo/pom.xml create mode 100644 design-demos/spring-state-demo/src/main/java/com/gsean/state/StateApp.java create mode 100644 design-demos/spring-state-demo/src/main/java/com/gsean/state/config/BizStateMachinePersist.java create mode 100644 design-demos/spring-state-demo/src/main/java/com/gsean/state/config/StateMachineConfig.java create mode 100644 design-demos/spring-state-demo/src/main/java/com/gsean/state/enums/TurnstileEvents.java create mode 100644 design-demos/spring-state-demo/src/main/java/com/gsean/state/enums/TurnstileStates.java create mode 100644 design-demos/spring-state-demo/src/test/java/com/gsean/state/StateAppTest.java create mode 100644 starter-demos/oss-aliyun-spring-boot-starter/pom.xml create mode 100644 starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/AliyunStarterApp.java create mode 100644 starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/config/AliyunOSSAutoConfig.java create mode 100644 starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/props/ALiYunCoreProperty.java create mode 100644 starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/props/AliYunOssProperty.java create mode 100644 starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/service/AliyunOssService.java create mode 100644 starter-demos/oss-aliyun-spring-boot-starter/src/main/resources/META-INF/spring.factories create mode 100644 starter-demos/oss-spring-boot-starter/pom.xml create mode 100644 starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/AliyunOSSConfig.java create mode 100644 starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/OssAutoConfig.java create mode 100644 starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/TencentOSSConfig.java create mode 100644 starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/constant/CommonConstant.java create mode 100644 starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/props/OSSCoreProperty.java create mode 100644 starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/OssService.java create mode 100644 starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/impl/AliyunOssServiceImpl.java create mode 100644 starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/impl/TencentOssServiceImpl.java create mode 100644 starter-demos/oss-spring-boot-starter/src/main/resources/META-INF/spring.factories create mode 100644 starter-demos/oss-tencent-spring-boot-starter/pom.xml create mode 100644 starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/TencentApp.java create mode 100644 starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/config/TencentAutoConfig.java create mode 100644 starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/props/TencentCoreProperty.java create mode 100644 starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/props/TencentOssProperty.java create mode 100644 starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/service/TencentOssService.java create mode 100644 starter-demos/oss-tencent-spring-boot-starter/src/main/resources/META-INF/spring.factories create mode 100644 starter-demos/pom.xml create mode 100644 starter-demos/starter-sample/pom.xml create mode 100644 starter-demos/starter-sample/src/main/java/com/gsean/starter/sample/StarterSampleApp.java create mode 100644 starter-demos/starter-sample/src/main/resources/application.yml create mode 100644 starter-demos/starter-sample/src/test/java/com/gsean/starter/sample/StarterSampleAppTest.java diff --git a/design-demos/pom.xml b/design-demos/pom.xml index f190034..7c84bce 100644 --- a/design-demos/pom.xml +++ b/design-demos/pom.xml @@ -19,6 +19,7 @@ design-proxy-demo design-adapter-demo creational-pattern-demos + spring-state-demo diff --git a/design-demos/spring-state-demo/pom.xml b/design-demos/spring-state-demo/pom.xml new file mode 100644 index 0000000..22f872c --- /dev/null +++ b/design-demos/spring-state-demo/pom.xml @@ -0,0 +1,34 @@ + + + + design-demos + com.gsean.demos + 1.0-SNAPSHOT + + 4.0.0 + + spring-state-demo + + + 8 + 8 + + + + + + + + org.springframework.statemachine + spring-statemachine-core + 2.0.3.RELEASE + + + org.springframework.boot + spring-boot-starter-web + + + + \ No newline at end of file diff --git a/design-demos/spring-state-demo/src/main/java/com/gsean/state/StateApp.java b/design-demos/spring-state-demo/src/main/java/com/gsean/state/StateApp.java new file mode 100644 index 0000000..1786a31 --- /dev/null +++ b/design-demos/spring-state-demo/src/main/java/com/gsean/state/StateApp.java @@ -0,0 +1,41 @@ +package com.gsean.state; + +import com.gsean.state.enums.TurnstileEvents; +import com.gsean.state.enums.TurnstileStates; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.statemachine.StateMachine; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/6 10:10 + * @modificed by + **/ +@SpringBootApplication +public class StateApp { + + public static void main(String[] args) { + SpringApplication.run(StateApp.class,args); + } + + +// @Autowired +// private StateMachine stateMachine; +// +// @Override +// public void run(String... strings) throws Exception { +// stateMachine.start(); +// System.out.println("--- coin ---"); +// stateMachine.sendEvent(TurnstileEvents.COIN); +// System.out.println("--- coin ---"); +// stateMachine.sendEvent(TurnstileEvents.COIN); +// System.out.println("--- push ---"); +// stateMachine.sendEvent(TurnstileEvents.PUSH); +// System.out.println("--- push ---"); +// stateMachine.sendEvent(TurnstileEvents.PUSH); +// stateMachine.stop(); +// } +} diff --git a/design-demos/spring-state-demo/src/main/java/com/gsean/state/config/BizStateMachinePersist.java b/design-demos/spring-state-demo/src/main/java/com/gsean/state/config/BizStateMachinePersist.java new file mode 100644 index 0000000..4d2bb6c --- /dev/null +++ b/design-demos/spring-state-demo/src/main/java/com/gsean/state/config/BizStateMachinePersist.java @@ -0,0 +1,39 @@ +package com.gsean.state.config; + +import com.gsean.state.enums.TurnstileEvents; +import com.gsean.state.enums.TurnstileStates; +import java.util.HashMap; +import java.util.Map; +import org.springframework.statemachine.StateMachineContext; +import org.springframework.statemachine.StateMachinePersist; +import org.springframework.statemachine.support.DefaultStateMachineContext; +import org.springframework.stereotype.Component; + +/** + * @authoer Jincheng.Guo11 + * @description 状态持久化 + * @date:created in 2021/12/6 13:33 + * @modificed by + **/ +@Component +public class BizStateMachinePersist implements + StateMachinePersist { + + static Map cache = new HashMap<>(16); + + @Override + public void write(StateMachineContext stateMachineContext, + Integer integer) throws Exception { + cache.put(integer, stateMachineContext.getState()); + + } + + @Override + public StateMachineContext read(Integer integer) + throws Exception { + // 注意状态机的初识状态与配置中定义的一致 + return cache.containsKey(integer) ? + new DefaultStateMachineContext<>(cache.get(integer), null, null, null, null, "turnstileStateMachine") : + new DefaultStateMachineContext<>(TurnstileStates.Locked, null, null, null, null, "turnstileStateMachine"); + } +} diff --git a/design-demos/spring-state-demo/src/main/java/com/gsean/state/config/StateMachineConfig.java b/design-demos/spring-state-demo/src/main/java/com/gsean/state/config/StateMachineConfig.java new file mode 100644 index 0000000..8f9b4c9 --- /dev/null +++ b/design-demos/spring-state-demo/src/main/java/com/gsean/state/config/StateMachineConfig.java @@ -0,0 +1,85 @@ +package com.gsean.state.config; + +import com.gsean.state.enums.TurnstileEvents; +import com.gsean.state.enums.TurnstileStates; +import java.util.EnumSet; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.statemachine.action.Action; +import org.springframework.statemachine.config.EnableStateMachine; +import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; +import org.springframework.statemachine.config.builders.StateMachineConfigBuilder; +import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer; +import org.springframework.statemachine.config.builders.StateMachineModelConfigurer; +import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; +import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; +import org.springframework.statemachine.persist.DefaultStateMachinePersister; +import org.springframework.statemachine.persist.StateMachinePersister; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/6 10:36 + * @modificed by + **/ +@Configuration +@EnableStateMachine +@Slf4j +public class StateMachineConfig extends EnumStateMachineConfigurerAdapter { + + + + + + @Override + public void configure( + StateMachineConfigurationConfigurer config) + throws Exception { + config.withConfiguration() + .machineId("turnstileStateMachine") + ; + } + + @Override + public void configure(StateMachineStateConfigurer states) + throws Exception { + states + .withStates() + // 初识状态:Locked + .initial(TurnstileStates.Locked) + .states(EnumSet.allOf(TurnstileStates.class)); + } + + @Override + public void configure( + StateMachineTransitionConfigurer transitions) + throws Exception { + transitions + .withExternal() + .source(TurnstileStates.Locked).target(TurnstileStates.Unlocked) + .event(TurnstileEvents.COIN).action(customerPassAndLock()) + .and() + .withExternal() + .source(TurnstileStates.Unlocked).target(TurnstileStates.Locked) + .event(TurnstileEvents.PUSH).action(turnstileUnlock()) + ; + } + + + + @Bean + public StateMachinePersister stateMachinePersist(BizStateMachinePersist bizStateMachinePersist) { + return new DefaultStateMachinePersister<>(bizStateMachinePersist); + } + + + public Action turnstileUnlock() { + return context -> System.out.println("解锁旋转门,以便游客能够通过" ); + } + + public Action customerPassAndLock() { + return context -> System.out.println("当游客通过,锁定旋转门" ); + } +} diff --git a/design-demos/spring-state-demo/src/main/java/com/gsean/state/enums/TurnstileEvents.java b/design-demos/spring-state-demo/src/main/java/com/gsean/state/enums/TurnstileEvents.java new file mode 100644 index 0000000..ccc89f3 --- /dev/null +++ b/design-demos/spring-state-demo/src/main/java/com/gsean/state/enums/TurnstileEvents.java @@ -0,0 +1,12 @@ +package com.gsean.state.enums; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/6 11:38 + * @modificed by + **/ +public enum TurnstileEvents { + // 推门,投币 + COIN, PUSH +} diff --git a/design-demos/spring-state-demo/src/main/java/com/gsean/state/enums/TurnstileStates.java b/design-demos/spring-state-demo/src/main/java/com/gsean/state/enums/TurnstileStates.java new file mode 100644 index 0000000..50fa382 --- /dev/null +++ b/design-demos/spring-state-demo/src/main/java/com/gsean/state/enums/TurnstileStates.java @@ -0,0 +1,12 @@ +package com.gsean.state.enums; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/6 11:38 + * @modificed by + **/ +public enum TurnstileStates { + // 解锁,锁定 + Unlocked, Locked +} diff --git a/design-demos/spring-state-demo/src/test/java/com/gsean/state/StateAppTest.java b/design-demos/spring-state-demo/src/test/java/com/gsean/state/StateAppTest.java new file mode 100644 index 0000000..ec3cf8d --- /dev/null +++ b/design-demos/spring-state-demo/src/test/java/com/gsean/state/StateAppTest.java @@ -0,0 +1,70 @@ +package com.gsean.state; + +import com.gsean.state.enums.TurnstileEvents; +import com.gsean.state.enums.TurnstileStates; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.statemachine.StateMachine; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/6 10:51 + * @modificed by + **/ +@RunWith(SpringRunner.class) +@SpringBootTest +public class StateAppTest { + + @Autowired + private StateMachine stateMachine; + + + @Before + public void init(){ + stateMachine.start(); + } + + @After + public void end(){ + stateMachine.stop(); + } + + /** + * 初始状态 : 锁定——coin——未锁定 + * + * @author: Jincheng.Guo11 + * @date: 2021/12/6 13:18 + * @return: void + * @throws: java.lang.Exception + * @modificed by: + */ + @Test + public void testA(){ + System.out.println("--------coin--------"); + stateMachine.sendEvent(TurnstileEvents.COIN); + } + + /** + * 解锁状态 : 解锁——coin——解锁 + * + * @author: Jincheng.Guo11 + * @date: 2021/12/6 13:31 + * @return: void + * @throws: java.lang.Exception + * @modificed by: + */ + @Test + public void testB(){ + System.out.println("---------coin-------------"); + stateMachine.sendEvent(TurnstileEvents.COIN); + System.out.println("---------coin-------------"); + stateMachine.sendEvent(TurnstileEvents.COIN); + + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index fd0a634..e2f2523 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,7 @@ mongodb-demes orm-demos excel-demo + starter-demos @@ -141,6 +142,7 @@ + diff --git a/starter-demos/oss-aliyun-spring-boot-starter/pom.xml b/starter-demos/oss-aliyun-spring-boot-starter/pom.xml new file mode 100644 index 0000000..2f777cf --- /dev/null +++ b/starter-demos/oss-aliyun-spring-boot-starter/pom.xml @@ -0,0 +1,37 @@ + + + + starter-demos + com.gsean.demos + 1.0-SNAPSHOT + + 4.0.0 + + oss-aliyun-spring-boot-starter + + + 8 + 8 + + + + + + com.aliyun.oss + aliyun-sdk-oss + 3.10.2 + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.springframework.boot + spring-boot-starter + + + + \ No newline at end of file diff --git a/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/AliyunStarterApp.java b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/AliyunStarterApp.java new file mode 100644 index 0000000..9cded69 --- /dev/null +++ b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/AliyunStarterApp.java @@ -0,0 +1,34 @@ +package com.gsean.starter; + +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 12:50 + * @modificed by + **/ +public class AliyunStarterApp { + + public static void main(String[] args) { + // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。 + String endpoint = "http://oss-cn-chengdu.aliyuncs.com"; +// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。 + String accessKeyId = "LTAI4G28ijo7sGUmidu9S5qB"; + String accessKeySecret = "H5Ba1UaYXO01DBKkEmGyLVDxagPzFL"; +// 填写Bucket名称,例如examplebucket。 + String bucketName = "common-file-test"; + +// 创建OSSClient实例。 + OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); + +// 创建存储空间。 + boolean b = ossClient.doesBucketExist(bucketName); + System.out.println(b); + +// 关闭OSSClient。 + ossClient.shutdown(); + } + +} diff --git a/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/config/AliyunOSSAutoConfig.java b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/config/AliyunOSSAutoConfig.java new file mode 100644 index 0000000..fe2c655 --- /dev/null +++ b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/config/AliyunOSSAutoConfig.java @@ -0,0 +1,40 @@ +package com.gsean.starter.config; + + +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.gsean.starter.props.AliYunOssProperty; +import com.gsean.starter.service.AliyunOssService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties({ AliYunOssProperty.class }) +public class AliyunOSSAutoConfig { + + @Autowired + private AliYunOssProperty aliYunOssProperty; + + public static final String OSS_SERVICE_BEAN_NAME = "aliyunOssService"; + + + + @Bean + @ConditionalOnMissingBean(name = OSS_SERVICE_BEAN_NAME) + public OSS ossClient(){ + String accessId = aliYunOssProperty.getAccessKeyId(); + String accessSecret = aliYunOssProperty.getSecret(); + String endPoint = aliYunOssProperty.getEndPoint(); + OSS oss = new OSSClientBuilder().build(endPoint, accessId, accessSecret); + return oss; + } + + + @Bean + public AliyunOssService aliyunOssService() { + return new AliyunOssService(aliYunOssProperty); + } +} diff --git a/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/props/ALiYunCoreProperty.java b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/props/ALiYunCoreProperty.java new file mode 100644 index 0000000..186d17c --- /dev/null +++ b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/props/ALiYunCoreProperty.java @@ -0,0 +1,13 @@ +package com.gsean.starter.props; + +import lombok.Data; + +@Data +public class ALiYunCoreProperty { + protected String regionId; + protected String accessKeyId; + protected String secret; + protected boolean enabled = false; + + +} \ No newline at end of file diff --git a/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/props/AliYunOssProperty.java b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/props/AliYunOssProperty.java new file mode 100644 index 0000000..e7cac78 --- /dev/null +++ b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/props/AliYunOssProperty.java @@ -0,0 +1,47 @@ +package com.gsean.starter.props; + +import java.time.Duration; +import java.util.LinkedHashMap; +import java.util.Map; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("gsean.aliyun.oss") +@Data +public class AliYunOssProperty extends ALiYunCoreProperty { + + /** + * 默认的bucket + */ + private String defaultBucket; + + /** + * 节点地址 + */ + private String endPoint; + + /** + * 代理过期时间 + */ + private Duration policyTimeout; + + /** + * 绑定域名,主要替换bucket与endpoint结合形成的域名 + */ + private Map domainNames = new LinkedHashMap<>(); + + + + public String getMainUrl() { + return getMainUrl(defaultBucket); + } + + public String getMainUrl(String bucket) { + String endPointSplit = endPoint.replaceFirst("https?://", ""); + String url = String.format("https://%s.%s", bucket, endPointSplit); + return url; + } + + + +} diff --git a/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/service/AliyunOssService.java b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/service/AliyunOssService.java new file mode 100644 index 0000000..ad0a897 --- /dev/null +++ b/starter-demos/oss-aliyun-spring-boot-starter/src/main/java/com/gsean/starter/service/AliyunOssService.java @@ -0,0 +1,49 @@ +package com.gsean.starter.service; + +import com.aliyun.oss.ClientException; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.OSSException; +import com.gsean.starter.props.AliYunOssProperty; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 13:07 + * @modificed by + **/ +public class AliyunOssService { + + @Autowired + private AliYunOssProperty aliYunOssProperty; + + @Autowired + private OSS oss; + + public AliyunOssService(AliYunOssProperty aliYunOssProperty) { + this.aliYunOssProperty = aliYunOssProperty; + } + + + + + /** + * 判断bucket是否存在 + * + * @param bucketname + * @author: Jincheng.Guo11 + * @date: 2021/12/8 13:12 + * @return: boolean + * @throws: java.lang.Exception + * @modificed by: + */ + public boolean exsistBucket(String bucketname){ + try { + return oss.doesBucketExist(bucketname); + } finally { + oss.shutdown(); + } + } + +} diff --git a/starter-demos/oss-aliyun-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starter-demos/oss-aliyun-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..33f3632 --- /dev/null +++ b/starter-demos/oss-aliyun-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gsean.starter.config.AliyunOSSAutoConfig \ No newline at end of file diff --git a/starter-demos/oss-spring-boot-starter/pom.xml b/starter-demos/oss-spring-boot-starter/pom.xml new file mode 100644 index 0000000..afbc4b0 --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/pom.xml @@ -0,0 +1,49 @@ + + + + starter-demos + com.gsean.demos + 1.0-SNAPSHOT + + 4.0.0 + + oss-spring-boot-starter + + + 8 + 8 + 3.10.2 + 5.6.54 + + + + + + com.aliyun.oss + aliyun-sdk-oss + ${aliyun-sdk.version} + + + com.qcloud + cos_api + ${tencent-sdk.version} + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.springframework.boot + spring-boot-starter + + + cn.hutool + hutool-core + 5.7.16 + + + + \ No newline at end of file diff --git a/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/AliyunOSSConfig.java b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/AliyunOSSConfig.java new file mode 100644 index 0000000..399115d --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/AliyunOSSConfig.java @@ -0,0 +1,31 @@ +package com.gsean.oss.config; + +import com.gsean.oss.constant.CommonConstant; +import com.gsean.oss.props.OSSCoreProperty; +import com.gsean.oss.service.OssService; +import com.gsean.oss.service.impl.AliyunOssServiceImpl; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/9 9:48 + * @modificed by + **/ +@Configuration(proxyBeanMethods = false) +@ConditionalOnProperty(prefix = CommonConstant.GLOBAL_PREFIX,name = CommonConstant.OSS_TYPE,havingValue = CommonConstant.ALIYUN) +public class AliyunOSSConfig { + + + @Bean + @ConditionalOnMissingBean({OssService.class}) + public OssService ossService(OSSCoreProperty ossCoreProperty){ + System.out.println("装载阿里云配置"); + return new AliyunOssServiceImpl(ossCoreProperty); + } + + +} diff --git a/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/OssAutoConfig.java b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/OssAutoConfig.java new file mode 100644 index 0000000..e39a524 --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/OssAutoConfig.java @@ -0,0 +1,39 @@ +package com.gsean.oss.config; + +import com.gsean.oss.constant.CommonConstant; +import com.gsean.oss.props.OSSCoreProperty; +import com.gsean.oss.service.OssService; +import com.gsean.oss.service.impl.AliyunOssServiceImpl; +import com.gsean.oss.service.impl.TencentOssServiceImpl; +import com.sun.scenario.effect.impl.sw.java.JSWColorAdjustPeer; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 15:13 + * @modificed by + **/ +@Configuration(proxyBeanMethods = false) +@EnableConfigurationProperties({ OSSCoreProperty.class}) +@Import({AliyunOSSConfig.class,TencentOSSConfig.class}) +public class OssAutoConfig { + + + + + + + + + + + + + + +} diff --git a/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/TencentOSSConfig.java b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/TencentOSSConfig.java new file mode 100644 index 0000000..9985043 --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/config/TencentOSSConfig.java @@ -0,0 +1,28 @@ +package com.gsean.oss.config; + +import com.gsean.oss.constant.CommonConstant; +import com.gsean.oss.props.OSSCoreProperty; +import com.gsean.oss.service.OssService; +import com.gsean.oss.service.impl.TencentOssServiceImpl; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/9 9:48 + * @modificed by + **/ +@Configuration(proxyBeanMethods = false) +@ConditionalOnProperty(prefix = CommonConstant.GLOBAL_PREFIX,name = CommonConstant.OSS_TYPE,havingValue = CommonConstant.TENCENT) +public class TencentOSSConfig { + @Bean + @ConditionalOnMissingBean({OssService.class}) + public OssService ossService(OSSCoreProperty ossCoreProperty){ + System.out.println("装载腾讯配置"); + return new TencentOssServiceImpl(ossCoreProperty); + } + +} diff --git a/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/constant/CommonConstant.java b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/constant/CommonConstant.java new file mode 100644 index 0000000..f930dcb --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/constant/CommonConstant.java @@ -0,0 +1,17 @@ +package com.gsean.oss.constant; + +/** + * @author Seepine + */ +public interface CommonConstant { + String TENCENT = "tencent"; + String ALIYUN = "aliyun"; + + String COS_URL_FORMAT = "https://%s.cos.%s.myqcloud.com/%s"; + + String GLOBAL_PREFIX="gsean.oss"; + + String OSS_TYPE="type"; + + +} diff --git a/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/props/OSSCoreProperty.java b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/props/OSSCoreProperty.java new file mode 100644 index 0000000..f9bc1dc --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/props/OSSCoreProperty.java @@ -0,0 +1,47 @@ +package com.gsean.oss.props; + +import com.gsean.oss.constant.CommonConstant; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@Data +@ConfigurationProperties(CommonConstant.GLOBAL_PREFIX) +public class OSSCoreProperty { + + // api访问id + private String accessKeyId; + // api访问密钥 + private String secret; + // 是否可用 + private boolean enabled = true; + // oss类型,默认腾讯云 + private String type= CommonConstant.TENCENT; + + private String bucketName; + + private String prefix="/test"; + + private final OSSCoreProperty.Tencent tencent = new OSSCoreProperty.Tencent(); + + private final OSSCoreProperty.Aliyun aliyun= new OSSCoreProperty.Aliyun(); + + // 腾讯个性化配置 + @Data + public static class Tencent { + private String region; + } + + // 阿里云个性化配置 + @Data + public static class Aliyun { + /** + * 节点地址 + */ + private String endPoint; + } + + + + + +} \ No newline at end of file diff --git a/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/OssService.java b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/OssService.java new file mode 100644 index 0000000..22c36b7 --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/OssService.java @@ -0,0 +1,53 @@ +package com.gsean.oss.service; + +import java.io.File; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 15:18 + * @modificed by + **/ +public interface OssService { + + /** + * 上传文件 + * + * @param bucket + * @param prefix + * @param file + * @author: Jincheng.Guo11 + * @date: 2021/12/8 15:25 + * @return: java.lang.String + * @throws: java.lang.Exception + * @modificed by: + */ + String upload(String bucket,String prefix, File file); + + /** + * 判断桶是否存在 + * + * @param bucketName + * @author: Jincheng.Guo11 + * @date: 2021/12/8 15:50 + * @return: boolean + * @throws: java.lang.Exception + * @modificed by: + */ + boolean exsistBucket(String bucketName); + + /** + * 获取对象url + * + * @param bucket + * @param prefix + * @param fileName + * @author: Jincheng.Guo11 + * @date: 2021/12/8 16:55 + * @return: java.lang.String + * @throws: java.lang.Exception + * @modificed by: + */ + String getObjectUrl(String bucket,String prefix,String fileName); + +} diff --git a/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/impl/AliyunOssServiceImpl.java b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/impl/AliyunOssServiceImpl.java new file mode 100644 index 0000000..d969dbd --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/impl/AliyunOssServiceImpl.java @@ -0,0 +1,54 @@ +package com.gsean.oss.service.impl; + +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.gsean.oss.props.OSSCoreProperty; +import com.gsean.oss.service.OssService; +import com.qcloud.cos.COSClient; +import com.qcloud.cos.ClientConfig; +import com.qcloud.cos.auth.BasicCOSCredentials; +import com.qcloud.cos.region.Region; +import java.io.File; +import javax.annotation.PostConstruct; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 15:29 + * @modificed by + **/ +public class AliyunOssServiceImpl implements OssService { + + private OSSCoreProperty ossCoreProperty; + + private OSS oss; + + public AliyunOssServiceImpl(OSSCoreProperty ossCoreProperty) { + this.ossCoreProperty = ossCoreProperty; + } + + @PostConstruct + public void init() { + String accessId = ossCoreProperty.getAccessKeyId(); + String accessSecret = ossCoreProperty.getSecret(); + String endPoint = ossCoreProperty.getAliyun().getEndPoint(); + this.oss = new OSSClientBuilder().build(endPoint, accessId, accessSecret); + } + + + + @Override + public String upload(String bucket, String fileName, File file) { + return null; + } + + @Override + public boolean exsistBucket(String bucketName) { + return oss.doesBucketExist(bucketName); + } + + @Override + public String getObjectUrl(String bucket, String prefix, String fileName) { + return null; + } +} diff --git a/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/impl/TencentOssServiceImpl.java b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/impl/TencentOssServiceImpl.java new file mode 100644 index 0000000..79250b6 --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/src/main/java/com/gsean/oss/service/impl/TencentOssServiceImpl.java @@ -0,0 +1,74 @@ +package com.gsean.oss.service.impl; + +import cn.hutool.core.io.file.FileNameUtil; +import cn.hutool.core.util.ObjectUtil; +import com.gsean.oss.constant.CommonConstant; +import com.gsean.oss.props.OSSCoreProperty; +import com.gsean.oss.service.OssService; +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.GeneratePresignedUrlRequest; +import com.qcloud.cos.model.GetObjectRequest; +import com.qcloud.cos.model.PutObjectRequest; +import com.qcloud.cos.model.PutObjectResult; +import com.qcloud.cos.region.Region; +import java.io.File; +import java.util.Date; +import javax.annotation.PostConstruct; +import lombok.extern.slf4j.Slf4j; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 15:29 + * @modificed by + **/ +@Slf4j +public class TencentOssServiceImpl implements OssService { + private OSSCoreProperty ossCoreProperty; + + private COSClient cosClient; + + public TencentOssServiceImpl(OSSCoreProperty ossCoreProperty) { + this.ossCoreProperty = ossCoreProperty; + } + + @PostConstruct + public void init() { + this.cosClient = new COSClient( + new BasicCOSCredentials(ossCoreProperty.getAccessKeyId(), ossCoreProperty.getSecret()), + new ClientConfig(new Region(ossCoreProperty.getTencent().getRegion()))); + } + + + + @Override + public String upload(String bucket, String prefix, File file) { + String name = file.getName(); + String key= ObjectUtil.isEmpty(prefix)?ossCoreProperty.getPrefix()+"/"+name:prefix+"/"+name; + PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, file); + PutObjectResult putObjectResult = this.cosClient.putObject(putObjectRequest); + return putObjectResult == null ? null : String.format(CommonConstant.COS_URL_FORMAT, bucket, ossCoreProperty.getTencent().getRegion(), key); + } + + @Override + public boolean exsistBucket(String bucketName) { + return this.cosClient.doesBucketExist(bucketName); + } + + @Override + public String getObjectUrl(String bucket, String prefix, String fileName) { + Date expiration = new Date(System.currentTimeMillis() + 3600000L); + String key= ObjectUtil.isEmpty(prefix)?ossCoreProperty.getPrefix()+"/"+fileName:prefix+"/"+fileName; + GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, key); + generatePresignedUrlRequest.setExpiration(expiration); + String url = this.cosClient.generatePresignedUrl(generatePresignedUrlRequest).toString(); + log.info("COS:获取文件下载URL:{}", url); + return url; + } +} diff --git a/starter-demos/oss-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starter-demos/oss-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..e0dad85 --- /dev/null +++ b/starter-demos/oss-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gsean.oss.config.OssAutoConfig \ No newline at end of file diff --git a/starter-demos/oss-tencent-spring-boot-starter/pom.xml b/starter-demos/oss-tencent-spring-boot-starter/pom.xml new file mode 100644 index 0000000..7d194eb --- /dev/null +++ b/starter-demos/oss-tencent-spring-boot-starter/pom.xml @@ -0,0 +1,37 @@ + + + + starter-demos + com.gsean.demos + 1.0-SNAPSHOT + + 4.0.0 + + oss-tencent-spring-boot-starter + + + 8 + 8 + + + + + + com.qcloud + cos_api + 5.6.54 + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.springframework.boot + spring-boot-starter + + + + \ No newline at end of file diff --git a/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/TencentApp.java b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/TencentApp.java new file mode 100644 index 0000000..75a6baa --- /dev/null +++ b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/TencentApp.java @@ -0,0 +1,38 @@ +package com.gsean.starter; + +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.region.Region; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 13:58 + * @modificed by + **/ +public class TencentApp { + + public static void main(String[] args) { + // 1 初始化用户身份信息(secretId, secretKey)。 +// SECRETID和SECRETKEY请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理 + String secretId = "AKIDNXR4TbT4PUIp78sKVQ4RXNvDF4exzjDQ"; + String secretKey = "xd2A1bvxiCOL1RaT5QTGlzwc2A1bw0nI"; + COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); +// 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 +// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。 + Region region = new Region("ap-chengdu"); + ClientConfig clientConfig = new ClientConfig(region); +// 这里建议设置使用 https 协议 +// 从 5.6.54 版本开始,默认使用了 https + clientConfig.setHttpProtocol(HttpProtocol.https); +// 3 生成 cos 客户端。 + COSClient cosClient = new COSClient(cred, clientConfig); + boolean test = cosClient.doesBucketExist("common-file-tencent-12"); +// boolean test = cosClient.doesBucketExist("common-file-tencent-1302654654"); + System.out.println(test); + } + +} diff --git a/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/config/TencentAutoConfig.java b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/config/TencentAutoConfig.java new file mode 100644 index 0000000..9b104e2 --- /dev/null +++ b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/config/TencentAutoConfig.java @@ -0,0 +1,28 @@ +package com.gsean.starter.config; + + +import com.gsean.starter.props.TencentOssProperty; +import com.gsean.starter.service.TencentOssService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties({ TencentOssProperty.class }) +public class TencentAutoConfig { + + @Autowired + private TencentOssProperty tencentOssProperty; + + + + + + + + @Bean + public TencentOssService tencentOssService() { + return new TencentOssService(tencentOssProperty); + } +} diff --git a/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/props/TencentCoreProperty.java b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/props/TencentCoreProperty.java new file mode 100644 index 0000000..3626644 --- /dev/null +++ b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/props/TencentCoreProperty.java @@ -0,0 +1,13 @@ +package com.gsean.starter.props; + +import lombok.Data; + +@Data +public class TencentCoreProperty { + protected String regionId; + protected String accessKeyId; + protected String secret; + protected boolean enabled = false; + + +} \ No newline at end of file diff --git a/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/props/TencentOssProperty.java b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/props/TencentOssProperty.java new file mode 100644 index 0000000..86a544e --- /dev/null +++ b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/props/TencentOssProperty.java @@ -0,0 +1,47 @@ +package com.gsean.starter.props; + +import java.time.Duration; +import java.util.LinkedHashMap; +import java.util.Map; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("gsean.tencent.oss") +@Data +public class TencentOssProperty extends TencentCoreProperty { + + /** + * 默认的bucket + */ + private String defaultBucket; + + /** + * 节点地址 + */ + private String endPoint; + + /** + * 代理过期时间 + */ + private Duration policyTimeout; + + /** + * 绑定域名,主要替换bucket与endpoint结合形成的域名 + */ + private Map domainNames = new LinkedHashMap<>(); + + + + public String getMainUrl() { + return getMainUrl(defaultBucket); + } + + public String getMainUrl(String bucket) { + String endPointSplit = endPoint.replaceFirst("https?://", ""); + String url = String.format("https://%s.%s", bucket, endPointSplit); + return url; + } + + + +} diff --git a/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/service/TencentOssService.java b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/service/TencentOssService.java new file mode 100644 index 0000000..f80be45 --- /dev/null +++ b/starter-demos/oss-tencent-spring-boot-starter/src/main/java/com/gsean/starter/service/TencentOssService.java @@ -0,0 +1,67 @@ +package com.gsean.starter.service; + +import com.gsean.starter.props.TencentOssProperty; +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.region.Region; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 13:07 + * @modificed by + **/ +public class TencentOssService { + + private TencentOssProperty tencentOssProperty; + + + + public TencentOssService(TencentOssProperty tencentOssProperty) { + this.tencentOssProperty = tencentOssProperty; + } + + + + /** + * 获取一个ossclient + * + * @return + */ + public COSClient createOss() { + COSCredentials cred = new BasicCOSCredentials(tencentOssProperty.getAccessKeyId(), + tencentOssProperty.getSecret()); + Region region = new Region("ap-chengdu"); + ClientConfig clientConfig = new ClientConfig(region); + clientConfig.setHttpProtocol(HttpProtocol.https); + COSClient cosClient = new COSClient(cred, clientConfig); + return cosClient; + } + + + + + /** + * 判断bucket是否存在 + * + * @param bucketname + * @author: Jincheng.Guo11 + * @date: 2021/12/8 13:12 + * @return: boolean + * @throws: java.lang.Exception + * @modificed by: + */ + public boolean exsistBucket(String bucketname){ + COSClient oss=null; + try { + oss = createOss(); + return oss.doesBucketExist(bucketname); + } finally { + oss.shutdown(); + } + } + +} diff --git a/starter-demos/oss-tencent-spring-boot-starter/src/main/resources/META-INF/spring.factories b/starter-demos/oss-tencent-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..a9ac781 --- /dev/null +++ b/starter-demos/oss-tencent-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gsean.starter.config.TencentAutoConfig \ No newline at end of file diff --git a/starter-demos/pom.xml b/starter-demos/pom.xml new file mode 100644 index 0000000..124aabb --- /dev/null +++ b/starter-demos/pom.xml @@ -0,0 +1,26 @@ + + + + gsean-springboot-demos + com.gsean.demos + 1.0-SNAPSHOT + + 4.0.0 + + starter-demos + pom + + oss-aliyun-spring-boot-starter + starter-sample + oss-tencent-spring-boot-starter + oss-spring-boot-starter + + + + 8 + 8 + + + \ No newline at end of file diff --git a/starter-demos/starter-sample/pom.xml b/starter-demos/starter-sample/pom.xml new file mode 100644 index 0000000..cdfaf66 --- /dev/null +++ b/starter-demos/starter-sample/pom.xml @@ -0,0 +1,44 @@ + + + + starter-demos + com.gsean.demos + 1.0-SNAPSHOT + + 4.0.0 + + starter-sample + + + + 8 + 8 + + + + + + + com.gsean.demos + oss-aliyun-spring-boot-starter + 1.0-SNAPSHOT + + + com.gsean.demos + oss-tencent-spring-boot-starter + 1.0-SNAPSHOT + + + com.gsean.demos + oss-spring-boot-starter + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-web + + + + \ No newline at end of file diff --git a/starter-demos/starter-sample/src/main/java/com/gsean/starter/sample/StarterSampleApp.java b/starter-demos/starter-sample/src/main/java/com/gsean/starter/sample/StarterSampleApp.java new file mode 100644 index 0000000..f3affa1 --- /dev/null +++ b/starter-demos/starter-sample/src/main/java/com/gsean/starter/sample/StarterSampleApp.java @@ -0,0 +1,19 @@ +package com.gsean.starter.sample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 13:19 + * @modificed by + **/ +@SpringBootApplication +public class StarterSampleApp { + + public static void main(String[] args) { + SpringApplication.run(StarterSampleApp.class,args); + } + +} diff --git a/starter-demos/starter-sample/src/main/resources/application.yml b/starter-demos/starter-sample/src/main/resources/application.yml new file mode 100644 index 0000000..6b07f6a --- /dev/null +++ b/starter-demos/starter-sample/src/main/resources/application.yml @@ -0,0 +1,29 @@ +gsean.aliyun.oss: + accessKeyId: LTAI4G28ijo7sGUmidu9S5qB + secret: H5Ba1UaYXO01DBKkEmGyLVDxagPzFL + enabled: true + endPoint: http://oss-cn-chengdu.aliyuncs.com + + +--- +gsean.tencent.oss: + accessKeyId: AKIDNXR4TbT4PUIp78sKVQ4RXNvDF4exzjDQ + secret: xd2A1bvxiCOL1RaT5QTGlzwc2A1bw0nI + enabled: true +# endPoint: http://oss-cn-chengdu.aliyuncs.com + +--- +gsean.oss: +# accessKeyId: AKIDNXR4TbT4PUIp78sKVQ4RXNvDF4exzjDQ +# secret: xd2A1bvxiCOL1RaT5QTGlzwc2A1bw0nI + accessKeyId: LTAI4G28ijo7sGUmidu9S5qB + secret: H5Ba1UaYXO01DBKkEmGyLVDxagPzFL + enabled: true +# type: tencent + type: aliyun + aliyun: + endPoint: http://oss-cn-chengdu.aliyuncs.com + +# tencent: +# region: ap-chengdu + diff --git a/starter-demos/starter-sample/src/test/java/com/gsean/starter/sample/StarterSampleAppTest.java b/starter-demos/starter-sample/src/test/java/com/gsean/starter/sample/StarterSampleAppTest.java new file mode 100644 index 0000000..56ee330 --- /dev/null +++ b/starter-demos/starter-sample/src/test/java/com/gsean/starter/sample/StarterSampleAppTest.java @@ -0,0 +1,70 @@ +package com.gsean.starter.sample; + +import com.gsean.oss.service.OssService; +import com.gsean.starter.service.AliyunOssService; +import com.gsean.starter.service.TencentOssService; +import java.io.File; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @authoer Jincheng.Guo11 + * @description + * @date:created in 2021/12/8 13:19 + * @modificed by + **/ +@SpringBootTest +@RunWith(SpringRunner.class) +public class StarterSampleAppTest { + + @Autowired + private TencentOssService tencentOssService; + @Autowired + private AliyunOssService aliyunOssService; + + + private final static String TENCENT_BUCKET="common-file-tencent-1302654654"; + + + @Autowired + private OssService ossService; + + @Test + public void testA(){ + System.out.println(aliyunOssService.exsistBucket("common-file-test")); + + } + + + @Test + public void testB(){ + System.out.println(tencentOssService.exsistBucket("common-file-tencent-1302654654")); + } + + + @Test + public void test3(){ +// System.out.println(ossService.exsistBucket("common-file-tencent-1302654654")); + System.out.println(ossService.exsistBucket("common-file-test")); + } + + + @Test + public void testD(){ + String upload = ossService.upload(TENCENT_BUCKET, "", + new File("D:\\tmp\\logs\\running.2021-11-24.log")); + System.out.println(upload); + } + + + @Test + public void testE(){ + System.out.println( ossService.getObjectUrl(TENCENT_BUCKET,"/test","running.2021-11-24.log")); + } + + + +} \ No newline at end of file -- Gitee