本文包含以下内容:
创建第三方服务,实现OSS 功能
创建服务端签名直传对应接口
将OSS 接口配置到网关访问
前端调用获取签名
前端上传图片
1.创建第三方服务,实现OSS 功能
1)创建module<dependency> <groupId>person.zxc.mallgroupId> <artifactId>mail-commonartifactId> <version>0.0.1-SNAPSHOTversion> <exclusions> <exclusion> <groupId>com.baomidougroupId> <artifactId>mybatis-plus-boot-starterartifactId> exclusion> exclusions>dependency>
或引入nacos 配置中心和注册中心依赖
<dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>dependency><dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>dependency>
建立/resources/bootstrap.properties 指定注册中心、配置中心地址,和注册服务的名称
spring.application.name=mail-third-partyspring.cloud.nacos.config.server-addr=127.0.0.1:8848
配置启动端口和配置注册中心地址
spring: cloud: nacos: config: server-addr: 127.0.0.1:8848 application: name: mail-third-partyserver: port: 30000
开启服务发现
@EnableDiscoveryClient3)导入OSS依赖添加oss 依赖
<dependency> <groupId>com.aliyun.cloudgroupId> <artifactId>spring-cloud-start-alicloud-ossartifactId> <scope>testscope>dependency>
引入管理
spring-cloud-alibaba 的依赖管理
<dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-alibaba-dependenciesartifactId> <version>2.1.0.RELEASEversion> <type>pomtype> <scope>importscope> dependency> dependencies>dependencyManagement>
3)添加配置文件
spring: cloud: alicloud: access-key: <Your Access-Key> secret-key: <Your Secret-Key> oss: endpoint: <Your Endpoint>
4)测试,得到效果
InputStream inputStream = new FileInputStream("D:\\test.png"); ossClient.putObject("mail-study", "test2.png", inputStream);// 关闭OSSClient。 ossClient.shutdown(); System.out.println("上传成功");
2.创建服务端签名直传对应接口
参考官网: https://help.aliyun.com/document_detail/31926.html?spm=a2c4g.11186623.6.1561.727574b8nypEBI创建出代码如下,进行访问OSS@RestControllerpublic 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 policy(){ 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 = format+"/"; // 用户上传文件时指定的前缀。 Map<String, String> respMap = new HashMap<>(); 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); }}
访问:http://localhost:3000/oos/policy成功获得签名
3.将OSS 接口配置到网关访问
配置网关- id: third-party-route uri: lb://mail-third-party predicates: - Path=/api/thirdparty/** filters: - RewritePath=/api/thirdparty/(?/?.*), /$\{segment}
访问:http://localhost:88/api/thirdparty/oss/policy 获得效果
4.前端调用获取签名
调用接口获取签名之后,封装成一个Promise 对象,方便进行下一步操作return new Promise((resolve,reject)=>{ http({ url: http.adornUrl("/thirdparty/oss/policy"), method: "get", params: http.adornParams({}) }).then(({ data }) => { resolve(data); }) });
5.前端上传图片
上传图片使用Upload组件上传,参考官网:https://element.eleme.cn/#/zh-CN/component/upload1)引入组件import SingleUpload from "@/components/upload/singleUpload";
2)添加标签
<single-upload v-model="dataForm.logo">single-upload>
3)配置跨域
-END-
可以关注我的公众号,获取更多学习资料
点击“在看”,学多少都不会忘~
![86228f95f79a7e19eaac9b4c0ab784a3.png](https://i-blog.csdnimg.cn/blog_migrate/b932c5a8f8ea502f1648c52d1383b2cd.png)