原 OSS服务端签名后直传java
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1394
package com.mszl.thirdparty.controller;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.mszl.common.utils.DateUtils;
import com.mszl.common.utils.ReturnMsgUtils;
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.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
@RestController
@RequestMapping("/oss")
public class OssController {
@Value("${alibaba.cloud.access-key}")
private String accessId;
@Value("${alibaba.cloud.secret-key}")
private String accessKey;
@Value("${alibaba.cloud.oss.endpoint}")
private String endpoint;
@Value("${alibaba.cloud.oss.bucket}")
private String bucket;
/**
* 功能:OSS服务端签名
* @Author: zxb
* @Date: 2022-05-11 17:47:46
*/
@GetMapping("/sign")
public ReturnMsgUtils signature(){
String host="http://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
String callbackUrl="http://88.88.88.88.:8888"; // callbackUrl为 上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实信息。
String dt=String.valueOf(DateUtils.currentDate());
String dir=dt+"/"; // 用户上传文件时指定的前缀。
OSSClient client=new OSSClient(endpoint, accessId, accessKey);
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=client.generatePostPolicy(expiration, policyConds);
byte[] binaryData=postPolicy.getBytes("utf-8");
String encodedPolicy=BinaryUtil.toBase64String(binaryData);
String postSignature=client.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) {
System.out.println(e.getMessage());
}
ReturnMsgUtils result=ReturnMsgUtils.success(respMap);
return result;
}
}
配置文件如下
alibaba:
cloud:
access-key: LTXXXX
secret-key: pw1tjxxxx
oss:
endpoint: oss-cn-shanghai.aliyuncs.com
bucket: mszh-shop #此属性是我们自定义的
官网截图如下:
{
"code": 200,
"msg": "恭喜您, 操作成功",
"data": {
"accessid": "LTAI5tIPJKDFLSDJROEWDSLFJL",
"policy": "eyJleHBpcmF0a9uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIyMDIyLTA1LTExLyJdXX0=",
"signature": "P1TZB10Ctc36BmyrN9cMejMcIGY=",
"dir": "2022-05-11/",
"host": "http://mszh-shop.oss-cn-shanghai.aliyuncs.com",
"expire": "1652264085"
},
"extData": null
}
官方文档:
https://help.aliyun.com/document_detail/31926.html
https://help.aliyun.com/document_detail/91868.htm?spm=a2c4g.11186623.0.0.16075d3fnjOPNd#concept-ahk-rfz-2fb
2022-05-11 18:25:39 阅读(1124)
名师出品,必属精品 https://www.91mszl.com