改为json序列化
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package com.sczx.store.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.sczx.store.config.OSSConfig;
|
||||
import com.sczx.store.dto.OssPlicyReq;
|
||||
import com.sczx.store.dto.OssPolicyRes;
|
||||
@ -13,10 +14,7 @@ import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Base64;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@ -29,20 +27,11 @@ public class OssServiceImpl implements OssService {
|
||||
public OssPolicyRes getPolicy(OssPlicyReq ossPlicyReq) {
|
||||
|
||||
long expireEndTime = System.currentTimeMillis() + ossPlicyReq.getExpireTime() * 1000;
|
||||
Date expiration = new Date(expireEndTime);
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
String expire = df.format(expiration);
|
||||
Map<String, Object> policyMap = getStringObjectMap(ossPlicyReq, expireEndTime);
|
||||
|
||||
// 创建policy
|
||||
Map<String, Object> policyMap = new LinkedHashMap<>();
|
||||
policyMap.put("expiration", expire);
|
||||
|
||||
Map<String, Object> conditionMap = new LinkedHashMap<>();
|
||||
conditionMap.put("bucket", ossPlicyReq.getBucketName()); // 需要替换成你的 bucket 名称
|
||||
|
||||
policyMap.put("conditions", new Object[]{conditionMap});
|
||||
|
||||
String policy = Base64.getEncoder().encodeToString(policyMap.toString().getBytes(StandardCharsets.UTF_8));
|
||||
// 使用FastJSON序列化policy为标准JSON字符串
|
||||
String policyJson = JSON.toJSONString(policyMap);
|
||||
String policy = Base64.getEncoder().encodeToString(policyJson.getBytes(StandardCharsets.UTF_8));
|
||||
String signature = calculateSignature(ossConfig.getAccessKeySecret(), policy);
|
||||
|
||||
// 构造返回数据
|
||||
@ -51,11 +40,41 @@ public class OssServiceImpl implements OssService {
|
||||
response.setPolicy(policy);
|
||||
response.setSignature(signature);
|
||||
response.setDir(ossPlicyReq.getDir() + "/"); // 可以自定义上传目录
|
||||
response.setHost("https://" + ossPlicyReq.getBucketName() + "." + ossConfig.getRegion() + ".aliyuncs.com"); // 需要替换成你的 bucket 名称
|
||||
response.setHost("https://" + ossPlicyReq.getBucketName() + "." + ossConfig.getRegion() + ".aliyuncs.com");
|
||||
response.setExpire(expireEndTime / 1000);
|
||||
return response;
|
||||
}
|
||||
|
||||
private static Map<String, Object> getStringObjectMap(OssPlicyReq ossPlicyReq, long expireEndTime) {
|
||||
Date expiration = new Date(expireEndTime);
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
String expire = df.format(expiration);
|
||||
|
||||
// 创建policy - 使用标准的JSON结构
|
||||
Map<String, Object> policyMap = new HashMap<>();
|
||||
policyMap.put("expiration", expire);
|
||||
|
||||
// 构造conditions数组
|
||||
List<Object> conditions = new ArrayList<>();
|
||||
|
||||
// bucket条件
|
||||
Map<String, String> bucketCondition = new HashMap<>();
|
||||
bucketCondition.put("bucket", ossPlicyReq.getBucketName());
|
||||
conditions.add(bucketCondition);
|
||||
|
||||
// 可选:添加目录前缀条件
|
||||
if (ossPlicyReq.getDir() != null && !ossPlicyReq.getDir().isEmpty()) {
|
||||
List<String> startsWithCondition = new ArrayList<>();
|
||||
startsWithCondition.add("starts-with");
|
||||
startsWithCondition.add("$key");
|
||||
startsWithCondition.add(ossPlicyReq.getDir() + "/");
|
||||
conditions.add(startsWithCondition);
|
||||
}
|
||||
|
||||
policyMap.put("conditions", conditions);
|
||||
return policyMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算签名
|
||||
* @param accessKeySecret
|
||||
|
||||
Reference in New Issue
Block a user