tsurumure 4 mesi fa
parent
commit
6d1c0d7719

+ 1 - 1
src/main/java/com/backendsys/modules/sdk/douyincloud/tos/service/DouyinTosService.java

@@ -36,5 +36,5 @@ public interface DouyinTosService {
 
 
     // [抖音云TOS] URL 转存
-    SysFileResult uploadFileFromUrlToCOS(String url);
+    SysFileResult uploadFileFromUrlToTOS(String url);
 }

+ 78 - 14
src/main/java/com/backendsys/modules/sdk/douyincloud/tos/service/impl/DouyinTosServiceImpl.java

@@ -27,6 +27,8 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.math.BigDecimal;
@@ -118,11 +120,12 @@ public class DouyinTosServiceImpl implements DouyinTosService {
 
         // ------------------------------------------------------------------------------
 
+        InputStream inputStream = null;
+
         // 初始化 TosClient
         TOSV2 tos = new TOSV2ClientBuilder().build(REGION, ENDPOINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
 
         // TOSV2 提供的所有接口均会抛出 TosException 异常,需要使用 try-catch 进行捕获并处理。
-        InputStream inputStream = null;
         try{
 
             // 设置上传的桶名和对象名
@@ -152,13 +155,9 @@ public class DouyinTosServiceImpl implements DouyinTosService {
             result.setDomain(DOMAIN);
             return result;
 
-        } catch (TosClientException e) {
+        } catch (TosClientException | TosServerException e) {
             // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
-            System.out.println("putObject failed (TosClientException)");
-            throw new CustException(e.getMessage());
-        } catch (TosServerException e) {
-            // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
-            System.out.println("putObject failed (TosServerException)");
+            System.out.println("putObject failed (TosClientException / TosServerException)");
             throw new CustException(e.getMessage());
         } catch (Throwable t) {
             // 作为兜底捕获其他异常,一般不会执行到这里
@@ -358,14 +357,79 @@ public class DouyinTosServiceImpl implements DouyinTosService {
     /**
      * [抖音云TOS] URL 转存
      */
-    public SysFileResult uploadFileFromUrlToCOS(String url) {
-//        try {
+    public SysFileResult uploadFileFromUrlToTOS(String url) {
+
+        if (StrUtil.isEmpty(url)) throw new CustException("url 不能为空");
+
+        // 下载URL文件到本地
+        File downloadFile = CommonUtil.downloadLocalFromUrl(url);
+        // 获得URL文件后缀名
+        String suffix = CommonUtil.getFileSuffixFromUrl(url);
+
+        // 使用临时文件夹 (/temp) + UUID文件名.后缀名
+        String object_filename = CommonUtil.generateUUIDFilename(suffix);
+        String object_key = "temp/" + object_filename;
+        System.out.println("initiateMultipartUpload object: " + object_key);
+
+        // 初始化 TosClient
+        TOSV2 tos = new TOSV2ClientBuilder().build(REGION, ENDPOINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
+        try (FileInputStream inputStream = new FileInputStream(downloadFile)) {
+
+            // TOSV2 提供的所有接口均会抛出 TosException 异常,需要使用 try-catch 进行捕获并处理。
+            try {
+
+                // 设置上传的桶名和对象名
+                PutObjectInput putObjectInput = new PutObjectInput()
+                        .setBucket(BUCKET_NAME)
+                        .setKey(object_key)
+                        .setContent(inputStream);
+
+                // 以下代码展示如何处理进度条
+                // 在 ObjectMetaRequestOptions 中设置文件大小,可在进度条中显示 total 总长度,否则 DataTransferStatus.getTotalBytes 值为 -1。
+                ObjectMetaRequestOptions options = new ObjectMetaRequestOptions().setContentLength(downloadFile.length());
+                putObjectInput.setOptions(options);
+
+                // 自定义实现 DataTransferListener,实现进度条功能
+                DataTransferListener listener = getDataTransferListener(object_filename);
+                putObjectInput.setDataTransferListener(listener);
+
+                // 上传对象
+                PutObjectOutput output = tos.putObject(putObjectInput);
+
+                // 自定义返回结果实体
+                SysFileResult result = new SysFileResult();
+                result.setKey(object_key);
+                result.setRequest_id(output.getRequestInfo().getRequestId());
+                result.setE_tag(output.getEtag());
+                result.setDomain(DOMAIN);
+                return result;
+
+            } catch (TosClientException | TosServerException e) {
+                // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
+                System.out.println("putObject failed (TosClientException / TosServerException)");
+                throw new CustException(e.getMessage());
+            } catch (Throwable t) {
+                // 作为兜底捕获其他异常,一般不会执行到这里
+                System.out.println("putObject failed (Throwable)");
+                throw new CustException(t.getMessage());
+            } finally {
+                if (inputStream != null) {
+                    try {
+                        inputStream.close();
+                    } catch (IOException e) {
+                        // 忽略关闭流时的异常
+                    }
+                }
+                try {
+                    tos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
 
-            if (StrUtil.isEmpty(url)) throw new CustException("url 不能为空");
+        } catch (IOException e) {
+            throw new CustException(e.getMessage());
+        }
 
-//        } catch (IOException e) {
-//            throw new CustException(e.getMessage());
-//        }
-        return null;
     }
 }

+ 2 - 10
src/main/java/com/backendsys/modules/sdk/tencentcloud/cos/service/impl/TencentCosServiceImpl.java

@@ -378,7 +378,6 @@ public class TencentCosServiceImpl implements TencentCosService {
         String object_key = "temp/" + object_filename;
         System.out.println("initiateMultipartUpload object: " + object_key);
 
-        InputStream inputStream = null;
         COSClient cosClient = getClient();
         try{
 
@@ -400,17 +399,10 @@ public class TencentCosServiceImpl implements TencentCosService {
             return result;
 
         } catch (CosClientException e) {
-            throw new CustException(e.getMessage());
+            throw new CustException("CosClientException: " + e.getMessage());
         } catch (InterruptedException e) {
-            throw new CustException(e.getMessage());
+            throw new CustException("InterruptedException: " + e.getMessage());
         } finally {
-            if (inputStream != null) {
-                try {
-                    inputStream.close();
-                } catch (IOException e) {
-                    // 忽略关闭流时的异常
-                }
-            }
             if (cosClient != null) cosClient.shutdown();
         }
     }