|
@@ -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;
|
|
|
}
|
|
|
|