阿里云OOS对象存储

1. 开通阿里云对象存储

  1. 先进入阿里云对象存储控制台
  2. 创建一个Buket
  3. AccessKey 开通 (使用子用户的AccessKey)
    在这里插入图片描述
    创建用户
    在这里插入图片描述
    添加权限
    在这里插入图片描述

获取到 AccessKey ID 和 AccessKey Secret
在这里插入图片描述

2. 上传服务

获取pom依赖
在这里插入图片描述

 <dependency>
	  <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
  </dependency>

yml 配置

在这里插入图片描述

服务端上传

代码示例一: (后台上传)

@SpringBootTest
class FyjmallThirdPartyApplicationTests {

    @Autowired
    private OSS oss;
    @Test
    void contextLoads() throws FileNotFoundException {


// 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(objectName)。
        String content = "Hello OSS";
        InputStream inputStream = new FileInputStream("E:\\0d40c24b264aa511.jpg");
        oss.putObject("fyjmall", "0d40c24b264aa511.jpg", inputStream);

// 关闭OSSClient。
        oss.shutdown();
        System.out.println("上传完成");
    }
}

服务端生成签名,给前台直接上传

在这里插入图片描述

代码示例:

yml配置
在这里插入图片描述

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

允许跨域设置

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值