- JDK版本:1.8
- oss sdk版本:aliyun-sdk-oss3.15.2
- 服务端签名
long expireEndTime = System.currentTimeMillis() + 21600* 1000 ;
Date expiration = new Date(expireEndTime);
// 上传空间
String uploadDir = DateTimeUtils.getDateTime("yyyyMMdd", expiration);
PolicyConditions policyCons = new PolicyConditions();
policyCons.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyCons.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, uploadDir);
// 生成post请求策略
String postPolicy = ossClient().generatePostPolicy(expiration, policyCons);
byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
// 生成签名用于直连上传
String postSignature = ossClient().calculatePostSignature(postPolicy);
// 构建
OssPolicy ossPolicy = new OssPolicy();
ossPolicy.setPolicy(encodedPolicy);
ossPolicy.setAccessKeyId(ossConfig().getAccessKeyId());
ossPolicy.setSignature(postSignature);
ossPolicy.setExpireEndTime((expireEndTime - 28800 * 1000)/ 1000);
ossPolicy.setDir(uploadDir);
// 前端上传域名
ossPolicy.setHost(ossConfig().getHost());
let imgSignature = {}
if (!oldExpireEndTime || oldExpireEndTime {
if (res.statusCode === 204) resolve(host + "/" + key)
else reject()
},
fail: (err) => {
reject()
},
})
- 出现问题 Invalid according to Policy: Policy expired。policy的过期时间设置为六个小时了。
policy在oss中也是需要设置的,可以先检查一下。
这些事情太复杂,一时搞不明白。
开通oss服务和创建oss存储空间啦,可以参考文档意见