谷粒商城-云存储-创建子模块作为第三方整合模块

Posted 最小的帆也能远航

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷粒商城-云存储-创建子模块作为第三方整合模块相关的知识,希望对你有一定的参考价值。

创建子模块mall-third-party 整合阿里云OSS等第三方模块

<?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>
    <parent>
        <groupId>com.xxh</groupId>
        <artifactId>guli-mall</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>mall-third-party</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mall-third-party</name>
    <description>第三方服务整合</description>
    <dependencies>
      <dependency>
        <groupId>com.xxh</groupId>
        <artifactId>mall-common</artifactId>
      </dependency>
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
        <version>2.2.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:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.163.131:8848
    alicloud:
      access-key: LTAI4FwvfjSycd1APnuG9bjj
      secret-key: O6xaxyiWfSIitcOkSuK27ju4hXT5Hl
      oss:
        endpoint: oss-cn-beijing.aliyuncs.com
        bucket: gulimall-hello

  application:
    name: mall-third-party

server:
  port: 30000

bootstrap.yaml

spring:
  application:
    name: mall-third-party
  cloud:
    nacos:
      config:
        server-addr: 192.168.163.131:8848
        namespace: 90a6e497-bfb6-4784-8dd6-244c08b0708c
        file-extension: yaml
        extension-configs:
          - data-id: oss.yaml
            group: DEFAULT_GROUP
            refresh: true

服务端签名接口

package com.xxh.third.party.controller;

/**
 * @author: xxh
 * @date: 2021/3/7 19:29
 */
@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;

    /**
     * Oss 获取服务端签名
     * @return
     */
    @RequestMapping("/oss/policy")
    public R policy() 

        // https://gulimall-hello.oss-cn-beijing.aliyuncs.com/hahaha.jpg  host的格式为 bucketname.endpoint
        String host = "https://" + bucket + "." + 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 = null;
        try 
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            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(StandardCharsets.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());
        

        return R.ok().put("data", respMap);
    

使用网关服务统一接入

spring:
  application:
    name: mall-gateway
  cloud:
    gateway:
      routes:
        - id: third_party_route
          uri: lb://mall-third-party
          predicates:
            - Path=/api/third-party/**
          filters:
            - RewritePath=/api/third-party/(?<segment>.*),/$\\segment

以上是关于谷粒商城-云存储-创建子模块作为第三方整合模块的主要内容,如果未能解决你的问题,请参考以下文章

day06项目【整合阿里云OSS和Excel导入分类】

谷粒商城--品牌管理详情

阿里云OOS技术

谷粒商城笔记+踩坑——上架商品spu到ES索引库

谷粒商城_01_环境搭建

谷粒商城项目总结