• OSS对象存储


    package com.atguigu.gulimall.thirdparty.controller;

    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 com.atguigu.common.utils.R;
    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;

    @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

    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 = 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("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);
    }
    }
  • 相关阅读:
    Python网络爬虫与信息提取(一)——Requests库的安装、Requests库的get()方法、爬取网页的通用代码框架
    CodeForces#632Div.2C. Eugene and an array
    CodeForces#631Div.2B_Dreamoon Likes Permutations
    hdu6228Tree
    hdu6576Worker(最小公倍数)
    hdu6575Budget
    hdu6570Wave (暴力求解)
    poj2010 Moo University
    poj1258Agri-Net(最小生成树)
    poj2377Bad Cowtractors (最小生成树变形之——最大生成树)
  • 原文地址:https://www.cnblogs.com/returnZero/p/14405732.html
Copyright © 2020-2023  润新知