云存储-创建子模块作为第三方整合模块 创建子模块gulimall-third-party整合阿里云OSS等第三方模块 pom.xml ?xml version="1.0" encoding="UTF-8"? project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http:
云存储-创建子模块作为第三方整合模块
创建子模块gulimall-third-party整合阿里云OSS等第三方模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.com.codingce</groupId>
<artifactId>gulimall-third-party</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gulimall-third-party</name>
<description>第三方服务</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<!-- 公众项目 -->
<dependency>
<groupId>cn.com.codingce</groupId>
<artifactId>gulimall-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--排除包-->
<exclusions>
<exclusion>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--引入spring-cloud-starter-alicloud-oss-->
<!--导入失败,需指定为2.1.0RELEASE稳定版-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<!--<artifactId>aliyun-oss-spring-boot-starter</artifactId>-->
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yaml
spring:spring:
application:
name: gulimall-third-party
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# alicloud:
# access-key: "access-key"
# secret-key: "secret-key"
# oss:
# endpoint: "oss-cn-beijing.aliyuncs.com"
# # 本没有这个属性, 自己添加
# bucket: "codingce-product"
logging:
level:
cn.com.codingce: debug
server:
port: 30000
bootstrap.properties
spring.application.name=gulimall-third-party# 指定 nacos 配置中心地址
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# 配置中心环境切换
spring.cloud.nacos.config.namespace=e50dccbc-fda9-45d6-b1c2-199ce0cce31f
#spring.cloud.nacos.config.group=DEFAULT_GROUP
spring.cloud.nacos.config.shared-configs[0].data-id=oss.yml
spring.cloud.nacos.config.shared-configs[0].group=DEFAULT_GROUP
# 是否动态刷新
spring.cloud.nacos.config.shared-configs[0].refresh=true
服务端签名接口
OssController.java
package cn.com.codingce.thirdparty.controller;import cn.com.codingce.common.utils.R;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author mxz
*/
@RestController
public class OssController {
@Autowired
OSS ossClient;
@Value("${spring.cloud.alicloud.oss.endpoint}")
private String endpoint;
@Value("${spring.cloud.alicloud.oss.bucket}")
private String bucket;
@Value("${spring.cloud.alicloud.access-key}")
private String accessId;
@RequestMapping("/oss/policy")
public R polcy() {
// String accessId = "<yourAccessKeyId>"; // 请填写您的AccessKeyId。
// String accessKey = "<yourAccessKeySecret>"; // 请填写您的AccessKeySecret。
// String endpoint = "oss-cn-hangzhou.aliyuncs.com"; // 请填写您的 endpoint。
// String bucket = "codingce-product"; // 请填写您的 bucketname 。
String host = "https://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
// callbackUrl为 上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实信息。
//String callbackUrl = "http://88.88.88.88:8888";
String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
// String dir = "user-dir-prefix/"; // 用户上传文件时指定的前缀。
String dir = format + "/"; // 用户上传文件时指定的前缀。
// 创建OSSClient实例。
// OSS ossClient = new OSSClientBuilder().build(endpoint, accessId, accessKey);
Map<String, String> respMap = null;
try {
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
// PostObject请求最大可支持的文件大小为5 GB,即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = ossClient.calculatePostSignature(postPolicy);
respMap = new LinkedHashMap<String, String>();
respMap.put("accessid", accessId);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
// respMap.put("expire", formatISO8601Date(expiration));
} catch (Exception e) {
// Assert.fail(e.getMessage());
System.out.println(e.getMessage());
} finally {
ossClient.shutdown();
}
return R.ok().put("data", respMap);
}
}
使用网关服务统一接入
gulimall-gateway 服务 application.yml
注意配置规则断言要讲更精确的放在前面
application:
name: mall-gateway
cloud:
gateway:
routes:
## 第三方服务
- id: third_party_route
uri: lb://gulimall-third-party
predicates:
- Path=/api/thirdparty/**
filters:
- RewritePath=/api/thirdparty(?<segment>/?.*), /$\{segment}
项目预览
项目地址
由于github卡的严重,已将此项目放到gitee上
欢迎访问