云储存-阿里云对象存储

云存储-阿里云对象存储

一、概念

在这里插入图片描述

单体项目与分布式项目对比

在这里插入图片描述

上传方式

在这里插入图片描述后端生成防伪令牌签名

二、开通

注册并实名

在这里插入图片描述

三、开通对象存储,并创建新Bucket

3.1 创建Bucket

一个项目对应一个bucket
在这里插入图片描述

3.2 SDK选择

JAVA-SDK文档
在这里插入图片描述

3.3 创建子账号

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

3.4 账号授权

在这里插入图片描述
在这里插入图片描述

四、使用

4.1 引入依赖

<!--        阿里云oss存储服务-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
        </dependency>

4.2 application.yml

在这里插入图片描述

五、创建第三方服务微服务

5.1 创建

gulimall-third-party
在这里插入图片描述在这里插入图片描述

5.2 引入父依赖和common依赖,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.jyyy.gulimall</groupId>
        <artifactId>gulimall</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>gulimall-third-party</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gulimall-third-party</name>
    <description>第三方服务整合</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.jyyy.gulimall</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>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>

5.3 配置nacos

创建命名空间

在这里插入图片描述

创建application.yml

一般发现注册服务定义在application.yaml(先注册后配置)
在这里插入图片描述

创建bootstrap.yaml

配置中心的服务定义在bootstrap.yaml

spring:
  application:
    name: gulimall-third-party
  cloud:
    nacos:
      config:
        server-addr: 192.168.33.10:8848
        file-extension: yaml
        namespace: 633b9573-ac72-4d21-ac43-1376a417c911
        ## 拓展配置
        extension-configs:
          - data-id: oss.yaml #带后缀不用file-extension
              group: DEFAULT_GROUP # 自定义data-id所在的组
              refresh: true # 自定义是否动态刷新

六、服务端签名接口

文档

6.1 创建OssController

@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 policy(){

         //https://gulimall-hello.oss-cn-beijing.aliyuncs.com/hahaha.jpg  host的格式为 bucketname.endpoint
        String host = "https://"+bucket+"."+endpoint;

        // 自定义日期格式文件夹
        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;    // 过期时间30秒
            Date expiration = new Date(expireEndTime);

            PolicyConditions policyConds = new PolicyConditions();

            // PostObject请求最大可支持的文件大小为1048576000,即CONTENT_LENGTH_RANGE为1048576000。
            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>();
            // AK
            respMap.put("accessid", accessId);
            // 用户表单上传的策略(Policy)
            respMap.put("policy", encodedPolicy);
            // 签名
            respMap.put("signature", postSignature);
            // 上传文件时指定的前缀
            respMap.put("dir", dir);
            // oss保存文件的host
            respMap.put("host", host);
            // 过期时间
            respMap.put("expire", String.valueOf(expireEndTime / 1000));

        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            // 关闭oss客户端流
            ossClient.shutdown();
        }

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

七、使用网关服务统一接入

spring:
  application:
    name: gulimall-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.33.10:8848
    gateway:
      routes:
        - id: product_route
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

#        优先级比renrenfast高,放前面,不然被拦截
        - id: third_party_route
          uri: lb://gulimall-third-party
          predicates:
            - Path=/api/third-party/**
          filters:
            - RewritePath=/api/third-party/(?<segment>.*),/$\{segment}

        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}

server:
  port: 88

八、解决跨域问题

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值