caishunfeng commented on code in PR #13649:
URL: 
https://github.com/apache/dolphinscheduler/pull/13649#discussion_r1125964838


##########
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/log/remote/S3RemoteLogHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dolphinscheduler.common.log.remote;
+
+import org.apache.dolphinscheduler.common.constants.Constants;
+import org.apache.dolphinscheduler.common.utils.PropertyUtils;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import lombok.extern.slf4j.Slf4j;
+
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
+import com.amazonaws.auth.BasicAWSCredentials;
+import com.amazonaws.client.builder.AwsClientBuilder;
+import com.amazonaws.regions.Regions;
+import com.amazonaws.services.s3.AmazonS3;
+import com.amazonaws.services.s3.AmazonS3ClientBuilder;
+import com.amazonaws.services.s3.model.Bucket;
+import com.amazonaws.services.s3.model.S3Object;
+import com.amazonaws.services.s3.model.S3ObjectInputStream;
+
+@Slf4j
+public class S3RemoteLogHandler implements RemoteLogHandler, Closeable {
+
+    private String accessKeyId;
+
+    private String accessKeySecret;
+
+    private String region;
+
+    private String bucketName;
+
+    private String endPoint;
+
+    private AmazonS3 s3Client;
+
+    private static S3RemoteLogHandler instance;
+
+    private S3RemoteLogHandler() {
+
+    }
+
+    public static synchronized S3RemoteLogHandler getInstance() {
+        if (instance == null) {
+            instance = new S3RemoteLogHandler();
+            instance.init();
+        }
+
+        return instance;
+    }
+
+    public void init() {
+        accessKeyId = readAccessKeyID();
+        accessKeySecret = readAccessKeySecret();
+        region = readRegion();
+        bucketName = readBucketName();
+        endPoint = readEndPoint();
+        s3Client = buildS3Client();
+        checkBucketNameExists(bucketName);
+    }
+
+    protected AmazonS3 buildS3Client() {
+        if (!StringUtils.isEmpty(endPoint)) {
+            return AmazonS3ClientBuilder
+                    .standard()
+                    .withPathStyleAccessEnabled(true)
+                    .withEndpointConfiguration(new 
AwsClientBuilder.EndpointConfiguration(
+                            endPoint, Regions.fromName(region).getName()))
+                    .withCredentials(
+                            new AWSStaticCredentialsProvider(new 
BasicAWSCredentials(accessKeyId, accessKeySecret)))
+                    .build();
+        } else {
+            return AmazonS3ClientBuilder
+                    .standard()
+                    .withCredentials(
+                            new AWSStaticCredentialsProvider(new 
BasicAWSCredentials(accessKeyId, accessKeySecret)))
+                    .withRegion(Regions.fromName(region))
+                    .build();
+        }
+    }
+
+    @Override
+    public void close() throws IOException {
+        s3Client.shutdown();
+    }
+
+    @Override
+    public void sendRemoteLog(String logPath) {
+        String objectName = RemoteLogUtils.getObjectNameFromLogPath(logPath);
+
+        try {
+            log.info("send remote log {} to S3 {}", logPath, objectName);
+            s3Client.putObject(bucketName, objectName, new File(logPath));
+        } catch (Exception e) {
+            log.error("error while sending remote log {} to S3 {}", logPath, 
objectName, e);
+        }
+    }
+
+    @Override
+    public void getRemoteLog(String logPath) {
+        String objectName = RemoteLogUtils.getObjectNameFromLogPath(logPath);
+
+        try {
+            log.info("get remote log on S3 {} to {}", objectName, logPath);
+            S3Object o = s3Client.getObject(bucketName, objectName);
+            S3ObjectInputStream s3is = o.getObjectContent();
+            FileOutputStream fos = new FileOutputStream(logPath);
+            byte[] readBuf = new byte[1024];
+            int readLen = 0;
+            while ((readLen = s3is.read(readBuf)) > 0) {
+                fos.write(readBuf, 0, readLen);
+            }
+            s3is.close();
+            fos.close();
+        } catch (Exception e) {
+            log.error("error while getting remote log on S3 {} to {}", 
objectName, logPath, e);
+        }
+    }

Review Comment:
   It's better to use `try-with-resource` way.



##########
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/log/remote/S3RemoteLogHandler.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dolphinscheduler.common.log.remote;
+
+import org.apache.dolphinscheduler.common.constants.Constants;
+import org.apache.dolphinscheduler.common.utils.PropertyUtils;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import lombok.extern.slf4j.Slf4j;
+
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
+import com.amazonaws.auth.BasicAWSCredentials;
+import com.amazonaws.client.builder.AwsClientBuilder;
+import com.amazonaws.regions.Regions;
+import com.amazonaws.services.s3.AmazonS3;
+import com.amazonaws.services.s3.AmazonS3ClientBuilder;
+import com.amazonaws.services.s3.model.Bucket;
+import com.amazonaws.services.s3.model.S3Object;
+import com.amazonaws.services.s3.model.S3ObjectInputStream;
+
+@Slf4j
+public class S3RemoteLogHandler implements RemoteLogHandler, Closeable {
+
+    private String accessKeyId;
+
+    private String accessKeySecret;
+
+    private String region;
+
+    private String bucketName;
+
+    private String endPoint;
+
+    private AmazonS3 s3Client;
+
+    private static S3RemoteLogHandler instance;
+
+    private S3RemoteLogHandler() {
+
+    }
+
+    public static synchronized S3RemoteLogHandler getInstance() {
+        if (instance == null) {
+            instance = new S3RemoteLogHandler();
+            instance.init();
+        }
+
+        return instance;
+    }
+
+    public void init() {
+        accessKeyId = readAccessKeyID();
+        accessKeySecret = readAccessKeySecret();
+        region = readRegion();
+        bucketName = readBucketName();
+        endPoint = readEndPoint();
+        s3Client = buildS3Client();
+        checkBucketNameExists(bucketName);
+    }
+
+    protected AmazonS3 buildS3Client() {
+        if (!StringUtils.isEmpty(endPoint)) {

Review Comment:
   ```suggestion
           if (StringUtils.isNotEmpty(endPoint)) {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to