瀏覽代碼

fss sign api.

deason 1 年之前
父節點
當前提交
599c806d21

+ 16 - 6
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/fss/impl/AliyunOssService.java

@@ -191,20 +191,32 @@ public class AliyunOssService implements FssService {
 
     @Override
     public boolean deleteFile(String filePath) {
+        filePath = this.fixOssFilePath(filePath);
+        OSS client = this.getClient();
+
         try {
-            filePath = this.fixOssFilePath(filePath);
-            this.getClient().deleteObject(FssProperty.FSS_BUCKET, filePath);
+            client.deleteObject(FssProperty.FSS_BUCKET, filePath);
             return true;
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             return false;
+        } finally {
+            try {
+                client.shutdown();
+            } catch (Exception e) {
+                // ignore
+            }
         }
     }
 
     @Override
     public boolean existFile(String filePath) {
         filePath = this.fixOssFilePath(filePath);
-        return this.getClient().doesObjectExist(FssProperty.FSS_BUCKET, filePath);
+        OSS client = this.getClient();
+
+        boolean exist = client.doesObjectExist(FssProperty.FSS_BUCKET, filePath);
+        client.shutdown();
+        return exist;
     }
 
     /**
@@ -227,8 +239,7 @@ public class AliyunOssService implements FssService {
             request.setObjectType("file");
 
             RefreshObjectCachesResponse resp = client.getAcsResponse(request);
-
-            log.info("refreshFile:{} taskId:{}", fileUrls, resp != null ? resp.getRefreshTaskId() : "none");
+            log.info("refreshFile:{} reqId:{}", fileUrls, resp != null ? resp.getRequestId() : "none");
         } catch (Exception e) {
             log.error("refreshFile:{} err:{}", fileUrls, e.getMessage());
         }
@@ -293,7 +304,6 @@ public class AliyunOssService implements FssService {
         if (filePath.startsWith("/")) {
             return filePath.substring(1);
         }
-
         return filePath;
     }
 

+ 34 - 6
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/fss/impl/TencentCosService.java

@@ -11,6 +11,7 @@ import com.qcloud.cos.COSClient;
 import com.qcloud.cos.ClientConfig;
 import com.qcloud.cos.auth.BasicCOSCredentials;
 import com.qcloud.cos.auth.COSCredentials;
+import com.qcloud.cos.http.HttpMethodName;
 import com.qcloud.cos.http.HttpProtocol;
 import com.qcloud.cos.model.COSObject;
 import com.qcloud.cos.model.GetObjectRequest;
@@ -25,6 +26,10 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.InputStream;
+import java.net.URL;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 public class TencentCosService implements FssService {
 
@@ -176,20 +181,32 @@ public class TencentCosService implements FssService {
 
     @Override
     public boolean deleteFile(String filePath) {
+        filePath = this.fixCosFilePath(filePath);
+        COSClient client = this.getClient();
+
         try {
-            filePath = this.fixCosFilePath(filePath);
-            this.getClient().deleteObject(FssProperty.FSS_BUCKET, filePath);
+            client.deleteObject(FssProperty.FSS_BUCKET, filePath);
             return true;
         } catch (Exception e) {
             log.error(e.getMessage(), e);
             return false;
+        } finally {
+            try {
+                client.shutdown();
+            } catch (Exception e) {
+                // ignore
+            }
         }
     }
 
     @Override
     public boolean existFile(String filePath) {
         filePath = this.fixCosFilePath(filePath);
-        return this.getClient().doesObjectExist(FssProperty.FSS_BUCKET, filePath);
+        COSClient client = this.getClient();
+
+        boolean exist = client.doesObjectExist(FssProperty.FSS_BUCKET, filePath);
+        client.shutdown();
+        return exist;
     }
 
     @Override
@@ -200,11 +217,23 @@ public class TencentCosService implements FssService {
 
     @Override
     public FssSignInfo buildSign(String filePath) {
+        filePath = this.fixCosFilePath(filePath);
+        String fileUrl = FssProperty.FSS_URL_PREFIX + "/" + filePath;
+
+        Map<String, String> params = new HashMap<>();
+        params.put("key", filePath);
+
+        Date expirationTime = new Date(System.currentTimeMillis() + 10L * 60L * 1000L);
+        COSClient client = this.getClient();
+        URL url = client.generatePresignedUrl(FssProperty.FSS_BUCKET, filePath, expirationTime, HttpMethodName.PUT);
+        client.shutdown();
+
         FssSignInfo info = new FssSignInfo();
         info.setFsType(FssType.TENCENT_COS.name());
         info.setSignIdentifier(String.valueOf(System.currentTimeMillis()));
-        //todo
-
+        info.setFormParams(params);
+        info.setFormUrl(url.toString());
+        info.setAccessUrl(fileUrl);
         return info;
     }
 
@@ -236,7 +265,6 @@ public class TencentCosService implements FssService {
         if (filePath.startsWith("/")) {
             return filePath.substring(1);
         }
-
         return filePath;
     }
 

+ 2 - 2
examcloud-support/src/main/java/cn/com/qmth/examcloud/support/fss/model/FssSignInfo.java

@@ -9,13 +9,13 @@ public class FssSignInfo {
     @ApiModelProperty("文件存储类型")
     private String fsType;
 
-    @ApiModelProperty("文件存储-上传请求地址,支持POST")
+    @ApiModelProperty("文件存储-上传请求地址")
     private String formUrl;
 
     @ApiModelProperty("文件存储-上传请求签名标识")
     private String signIdentifier;
 
-    @ApiModelProperty("文件存储-上传请求表单参数列表,其中上传文件的参数名为file")
+    @ApiModelProperty("文件存储-上传请求表单参数列表")
     private Map<String, String> formParams;
 
     @ApiModelProperty("文件存储-当前文件的访问地址")

+ 2 - 2
examcloud-support/src/test/java/cn/com/qmth/examcloud/support/test/OssClientTest.java

@@ -52,7 +52,7 @@ public class OssClientTest {
         final String fileMd5 = "80bf42bd4d12b2a38eff3b32249b17fd";
 
         FssService fssService = FssFactory.getInstance(false);
-        // System.out.println(new JsonMapper().toJson(fssService.buildSign(filePath)));
+        System.out.println(new JsonMapper().toJson(fssService.buildSign(filePath)));
 
         // FssFileInfo result = fssService.writeFile(filePath, testFile, fileMd5);
         byte[] testBytes = IOUtils.toByteArray(Files.newInputStream(testFile.toPath()));
@@ -70,7 +70,7 @@ public class OssClientTest {
         // fssService.deleteFile(filePath);
     }
 
-    @Test
+    // @Test
     public void demoTest2() throws Exception {
         FssProperty.FSS_URL_PREFIX = "https://ecs-test-static.qmth.com.cn";