EricGao888 commented on code in PR #11708: URL: https://github.com/apache/dolphinscheduler/pull/11708#discussion_r962430143
########## dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OssOperator.java: ########## @@ -0,0 +1,321 @@ +/* + * 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.utils; + +import static org.apache.dolphinscheduler.common.Constants.ALIBABA_CLOUD_OSS_END_POINT; +import static org.apache.dolphinscheduler.common.Constants.FOLDER_SEPARATOR; +import static org.apache.dolphinscheduler.common.Constants.FORMAT_S_S; +import static org.apache.dolphinscheduler.common.Constants.RESOURCE_TYPE_FILE; +import static org.apache.dolphinscheduler.common.Constants.RESOURCE_TYPE_UDF; + +import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.common.enums.ResUploadType; +import org.apache.dolphinscheduler.common.storage.StorageOperate; +import org.apache.dolphinscheduler.plugin.task.api.TaskConstants; +import org.apache.dolphinscheduler.spi.enums.ResourceType; + +import org.apache.commons.lang3.StringUtils; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.Closeable; +import java.io.File; +import java.io.FileNotFoundException; +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; +import java.util.stream.Stream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.OSSException; +import com.aliyun.oss.model.Bucket; +import com.aliyun.oss.model.OSSObject; +import com.aliyun.oss.model.ObjectMetadata; +import com.aliyun.oss.model.PutObjectRequest; + +public class OssOperator implements Closeable, StorageOperate { + + private static final Logger logger = LoggerFactory.getLogger(OssOperator.class); + + public static String ACCESS_KEY_ID; + + public static String ACCESS_KEY_SECRET; + + public static String REGION; + + public static String BUCKET_NAME; + + public static String BASE_DIR = ""; + + private OSS ossClient; + + private PropertyUtilsWrapper propertyUtilsWrapper; + + private OssOperator() { + } + + private enum OssOperatorSingleton { + + INSTANCE; + + private final OssOperator instance; + + OssOperatorSingleton() { + instance = new OssOperator(); + } + + private OssOperator getInstance() { + return instance; + } + } + + public static OssOperator getInstance() { + return OssOperatorSingleton.INSTANCE.getInstance(); + } + + public void init() { + propertyUtilsWrapper = createPropertyUtilsWrapper(); + ACCESS_KEY_ID = propertyUtilsWrapper.getString(TaskConstants.ALIBABA_CLOUD_ACCESS_KEY_ID); + ACCESS_KEY_SECRET = propertyUtilsWrapper.getString(TaskConstants.ALIBABA_CLOUD_ACCESS_KEY_SECRET); + REGION = propertyUtilsWrapper.getString(TaskConstants.ALIBABA_CLOUD_REGION); + BUCKET_NAME = propertyUtilsWrapper.getString(Constants.ALIBABA_CLOUD_OSS_BUCKET_NAME); + + ossClient = buildOssClient(); + ensureBucketSuccessfullyCreated(BUCKET_NAME); + } + + @Override + public void close() throws IOException { + ossClient.shutdown(); + } + + @Override + public void createTenantDirIfNotExists(String tenantCode) throws Exception { + mkdir(tenantCode, getOssResDir(tenantCode)); + mkdir(tenantCode, getOssUdfDir(tenantCode)); + } + + @Override + public String getResDir(String tenantCode) { + return getOssResDir(tenantCode) + FOLDER_SEPARATOR; + } + + @Override + public String getUdfDir(String tenantCode) { + return getOssUdfDir(tenantCode) + FOLDER_SEPARATOR; + } + + @Override + public boolean mkdir(String tenantCode, String path) throws IOException { + final String key = path + FOLDER_SEPARATOR; + if (!ossClient.doesObjectExist(BUCKET_NAME, key)) { + createOssPrefix(BUCKET_NAME, key); + } + return true; + } + + protected void createOssPrefix(final String bucketName, final String key) { + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentLength(0); + InputStream emptyContent = new ByteArrayInputStream(new byte[0]); + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, emptyContent, metadata); + ossClient.putObject(putObjectRequest); + } + + @Override + public String getResourceFileName(String tenantCode, String fileName) { + if (fileName.startsWith(FOLDER_SEPARATOR)) { + fileName = fileName.replaceFirst(FOLDER_SEPARATOR, ""); + } + return String.format(FORMAT_S_S, getOssResDir(tenantCode), fileName); + } + + @Override + public String getFileName(ResourceType resourceType, String tenantCode, String fileName) { + if (fileName.startsWith(FOLDER_SEPARATOR)) { + fileName = fileName.replaceFirst(FOLDER_SEPARATOR, ""); + } + return getDir(resourceType, tenantCode) + fileName; + } + + @Override + 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()); + } + OSSObject ossObject = ossClient.getObject(BUCKET_NAME, srcFilePath); + try ( + InputStream ossInputStream = ossObject.getObjectContent(); + FileOutputStream fos = new FileOutputStream(dstFilePath)) { + byte[] readBuf = new byte[1024]; + int readLen; + while ((readLen = ossInputStream.read(readBuf)) > 0) { + fos.write(readBuf, 0, readLen); + } + } catch (OSSException e) { + throw new IOException(e.getMessage()); Review Comment: Sure, will fix it. -- 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]
