美文网首页
springcloud-alibaba-oss-服务器签名-we

springcloud-alibaba-oss-服务器签名-we

作者: 与诗小睡 | 来源:发表于2020-08-07 15:30 被阅读0次

    1 . 阿里云注册开通oss账号,并配置好权限阿里云

    1. 项目引入jar包:
      <!--spring-cloud-oss对象云存储-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
                <version>2.2.0.RELEASE</version>
            </dependency>
    

    3.编写controller发送签名:

    package com.poeam.guli.thirdpart.controller;
    
    import com.aliyun.oss.OSS;
    import com.aliyun.oss.OSSClientBuilder;
    import com.aliyun.oss.common.utils.BinaryUtil;
    import com.aliyun.oss.model.MatchMode;
    import com.aliyun.oss.model.PolicyConditions;
    import com.poeam.guli.common.utils.R;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.Date;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    @RestController
    public class PlocyController {
    
      @Autowired
      private OSS ossClient;
    
       @Value("${spring.cloud.alicloud.access-key}")
        private String accessId;
       @Value("${spring.cloud.alicloud.secret-key}")
       private String accessKey;
    
        @Value("${spring.cloud.alicloud.oss.endpoint}")
        private String endpoint;
    
        @Value("${spring.cloud.alicloud.oss.bukect}")
        private String bucket;
    
        @GetMapping("plocy")
        public R getPlocy () {
            String endpoint = "oss-cn-hangzhou.aliyuncs.com"; // 请填写您的 endpoint。
            String host = "https://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
            String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))+"/"; // 用户上传文件时指定的前缀。
            Map<String, String> respMap = new LinkedHashMap<String, String>();
            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.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));
            } catch (Exception e) {
                // Assert.fail(e.getMessage());
                System.out.println(e.getMessage());
            }
            return R.ok().put("data", respMap);
        }
    
    }
    

    postman测试获取签名结果

    相关文章

      网友评论

          本文标题:springcloud-alibaba-oss-服务器签名-we

          本文链接:https://www.haomeiwen.com/subject/kvifdktx.html