You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by zh...@apache.org on 2022/07/28 03:23:10 UTC

[dolphinscheduler] 02/02: [Fix-10665] [S3] Fix s3 download method (#10675)

This is an automated email from the ASF dual-hosted git repository.

zhongjiajie pushed a commit to branch 3.0.0-prepare
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git

commit 1f55cc89c14d775239cd2118cc717271738e3fcd
Author: Sheldon <39...@users.noreply.github.com>
AuthorDate: Wed Jul 27 21:25:13 2022 +0800

    [Fix-10665] [S3] Fix s3 download method (#10675)
    
    when overwrite param is true and dest file is exist, throw exception
    make dirs for ancestor directories of the source file path if it is not exists
    To describe the error more clearly, change "the file isn`t exists" to specific error message
    
    Co-authored-by: sheldonliu <sheldonliu>
    (cherry picked from commit efd68f480548839fe6c21dcdf3836afa42bc9029)
---
 .../apache/dolphinscheduler/common/utils/S3Utils.java  | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/S3Utils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/S3Utils.java
index 44cf5bf2d1..6c13e94451 100644
--- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/S3Utils.java
+++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/S3Utils.java
@@ -50,6 +50,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.nio.file.Files;
 import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -162,21 +163,26 @@ public class S3Utils implements Closeable, StorageOperate {
     }
 
     @Override
-    public void download(String tenantCode, String srcFilePath, String dstFile, boolean deleteSource, boolean overwrite) throws IOException {
+    public void download(String tenantCode, String srcFilePath, String dstFilePath, boolean deleteSource, boolean overwrite) throws IOException {
+        File dstFile = new File(dstFilePath);
+        if (dstFile.isDirectory()) {
+            Files.delete(dstFile.toPath());
+        } else {
+            Files.createDirectories(dstFile.getParentFile().toPath());
+        }
         S3Object o = s3Client.getObject(BUCKET_NAME, srcFilePath);
         try (S3ObjectInputStream s3is = o.getObjectContent();
-             FileOutputStream fos = new FileOutputStream(new File(dstFile))) {
+             FileOutputStream fos = new FileOutputStream(dstFilePath)) {
             byte[] readBuf = new byte[1024];
-            int readLen = 0;
+            int readLen;
             while ((readLen = s3is.read(readBuf)) > 0) {
                 fos.write(readBuf, 0, readLen);
             }
         } catch (AmazonServiceException e) {
-            logger.error("the resource can`t be downloaded,the bucket is {},and the src is {}", tenantCode, srcFilePath);
             throw new IOException(e.getMessage());
         } catch (FileNotFoundException e) {
-            logger.error("the file isn`t exists");
-            throw new IOException("the file isn`t exists");
+            logger.error("the destination file {} not found", dstFilePath);
+            throw e;
         }
     }