|
@@ -1,13 +1,17 @@
|
|
package cn.com.qmth.examcloud.support.fss.impl;
|
|
package cn.com.qmth.examcloud.support.fss.impl;
|
|
|
|
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
import cn.com.qmth.examcloud.support.fss.FssHelper;
|
|
import cn.com.qmth.examcloud.support.fss.FssHelper;
|
|
import cn.com.qmth.examcloud.support.fss.FssProperty;
|
|
import cn.com.qmth.examcloud.support.fss.FssProperty;
|
|
import cn.com.qmth.examcloud.support.fss.FssService;
|
|
import cn.com.qmth.examcloud.support.fss.FssService;
|
|
import cn.com.qmth.examcloud.support.fss.model.FssFileInfo;
|
|
import cn.com.qmth.examcloud.support.fss.model.FssFileInfo;
|
|
import cn.com.qmth.examcloud.support.fss.model.FssSignInfo;
|
|
import cn.com.qmth.examcloud.support.fss.model.FssSignInfo;
|
|
|
|
+import cn.com.qmth.examcloud.support.fss.model.FssType;
|
|
|
|
+import com.aliyun.oss.ClientBuilderConfiguration;
|
|
import com.aliyun.oss.OSS;
|
|
import com.aliyun.oss.OSS;
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
|
+import com.aliyun.oss.common.auth.ServiceSignature;
|
|
import com.aliyun.oss.model.GetObjectRequest;
|
|
import com.aliyun.oss.model.GetObjectRequest;
|
|
import com.aliyun.oss.model.OSSObject;
|
|
import com.aliyun.oss.model.OSSObject;
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
@@ -16,6 +20,7 @@ import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesRequest;
|
|
import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesResponse;
|
|
import com.aliyuncs.cdn.model.v20180510.RefreshObjectCachesResponse;
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.commons.lang3.time.DateUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
@@ -23,6 +28,10 @@ import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
|
+import java.util.Base64;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
public class AliyunOssService implements FssService {
|
|
public class AliyunOssService implements FssService {
|
|
|
|
|
|
@@ -227,16 +236,45 @@ public class AliyunOssService implements FssService {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public FssSignInfo buildSign(String filePath) {
|
|
public FssSignInfo buildSign(String filePath) {
|
|
- return new FssSignInfo();
|
|
|
|
|
|
+ filePath = this.fixOssFilePath(filePath);
|
|
|
|
+ String fileUrl = FssProperty.FSS_URL_PREFIX + "/" + filePath;
|
|
|
|
+
|
|
|
|
+ // 示例:https://{bucket}.oss-cn-shenzhen.aliyuncs.com
|
|
|
|
+ String requestUrl = FssProperty.FSS_ENDPOINT.replace(FssProperty.FSS_REGION_ID,
|
|
|
|
+ FssProperty.FSS_BUCKET + "." + FssProperty.FSS_REGION_ID);
|
|
|
|
+
|
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
|
+ params.put("key", filePath);
|
|
|
|
+ params.put("OSSAccessKeyId", FssProperty.FSS_ACCESS_KEY_ID);
|
|
|
|
+
|
|
|
|
+ long maxFileSize = 100L * 1024L * 1024L;
|
|
|
|
+ Date expireDate = DateUtils.addMinutes(new Date(), 10);
|
|
|
|
+ String expiration = DateUtil.format(expireDate, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
|
|
+ String policy = String.format("{\"expiration\":\"%s\",\"conditions\":[[\"content-length-range\",0,%s]]}",
|
|
|
|
+ expiration, maxFileSize);
|
|
|
|
+ String encodePolicy = Base64.getEncoder().encodeToString(policy.getBytes());
|
|
|
|
+ params.put("policy", encodePolicy);
|
|
|
|
+
|
|
|
|
+ String signature = ServiceSignature.create().computeSignature(FssProperty.FSS_ACCESS_KEY_SECRET, encodePolicy);
|
|
|
|
+ params.put("Signature", signature);
|
|
|
|
+
|
|
|
|
+ FssSignInfo info = new FssSignInfo();
|
|
|
|
+ info.setFsType(FssType.ALIYUN_OSS.name());
|
|
|
|
+ info.setSignIdentifier(String.valueOf(System.currentTimeMillis()));
|
|
|
|
+ info.setFormParams(params);
|
|
|
|
+ info.setFormUrl(requestUrl);
|
|
|
|
+ info.setAccessUrl(fileUrl);
|
|
|
|
+ return info;
|
|
}
|
|
}
|
|
|
|
|
|
private OSS getClient() {
|
|
private OSS getClient() {
|
|
try {
|
|
try {
|
|
- // ClientBuilderConfiguration configuration = new ClientBuilderConfiguration();
|
|
|
|
|
|
+ ClientBuilderConfiguration configuration = new ClientBuilderConfiguration();
|
|
|
|
+ configuration.setMaxErrorRetry(1);
|
|
|
|
|
|
OSS client = new OSSClientBuilder().build(
|
|
OSS client = new OSSClientBuilder().build(
|
|
this.internal ? FssProperty.FSS_INTERNAL_ENDPOINT : FssProperty.FSS_ENDPOINT,
|
|
this.internal ? FssProperty.FSS_INTERNAL_ENDPOINT : FssProperty.FSS_ENDPOINT,
|
|
- FssProperty.FSS_ACCESS_KEY_ID, FssProperty.FSS_ACCESS_KEY_SECRET);
|
|
|
|
|
|
+ FssProperty.FSS_ACCESS_KEY_ID, FssProperty.FSS_ACCESS_KEY_SECRET, configuration);
|
|
// client.setBucketTransferAcceleration(FssProperty.FSS_BUCKET, true);
|
|
// client.setBucketTransferAcceleration(FssProperty.FSS_BUCKET, true);
|
|
|
|
|
|
return client;
|
|
return client;
|