rickchengx commented on code in PR #13562: URL: https://github.com/apache/dolphinscheduler/pull/13562#discussion_r1133271693
########## dolphinscheduler-storage-plugin/dolphinscheduler-storage-gcs/src/main/java/org/apache/dolphinscheduler/plugin/storage/gcs/GcsStorageOperator.java: ########## @@ -0,0 +1,494 @@ +/* + * 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.plugin.storage.gcs; + +import static org.apache.dolphinscheduler.common.constants.Constants.EMPTY_STRING; +import static org.apache.dolphinscheduler.common.constants.Constants.FOLDER_SEPARATOR; +import static org.apache.dolphinscheduler.common.constants.Constants.FORMAT_S_S; +import static org.apache.dolphinscheduler.common.constants.Constants.RESOURCE_TYPE_FILE; +import static org.apache.dolphinscheduler.common.constants.Constants.RESOURCE_TYPE_UDF; + +import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.common.enums.ResUploadType; +import org.apache.dolphinscheduler.common.utils.PropertyUtils; +import org.apache.dolphinscheduler.plugin.storage.api.StorageEntity; +import org.apache.dolphinscheduler.plugin.storage.api.StorageOperate; +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.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.sql.Date; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import lombok.Data; +import lombok.extern.slf4j.Slf4j; + +import com.google.api.gax.paging.Page; +import com.google.auth.oauth2.ServiceAccountCredentials; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.BlobId; +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +@Data +@Slf4j +public class GcsStorageOperator implements Closeable, StorageOperate { + + private Storage gcsStorage; + + private String bucketName; + + private String credential; + + public GcsStorageOperator() { + + } + + public void init() { + try { + credential = readCredentials(); + bucketName = readBucketName(); + gcsStorage = buildGcsStorage(credential); + + checkBucketNameExists(bucketName); + } catch (IOException e) { + log.error("GCS Storage operator init failed", e); + } + } + + protected Storage buildGcsStorage(String credential) throws IOException { + return StorageOptions.newBuilder() + .setCredentials(ServiceAccountCredentials.fromStream( + Files.newInputStream(Paths.get(credential)))) + .build() + .getService(); + } + + protected String readCredentials() { + return PropertyUtils.getString(Constants.GOOGLE_CLOUD_STORAGE_CREDENTIAL); + } + + protected String readBucketName() { + return PropertyUtils.getString(Constants.GOOGLE_CLOUD_STORAGE_BUCKET_NAME); + } + + @Override + public void createTenantDirIfNotExists(String tenantCode) throws Exception { + mkdir(tenantCode, getGcsResDir(tenantCode)); + mkdir(tenantCode, getGcsUdfDir(tenantCode)); + } + + @Override + public String getResDir(String tenantCode) { + return getGcsResDir(tenantCode) + FOLDER_SEPARATOR; + } + + @Override + public String getUdfDir(String tenantCode) { + return getGcsUdfDir(tenantCode) + FOLDER_SEPARATOR; + } + + @Override + public String getResourceFileName(String tenantCode, String fileName) { + if (fileName.startsWith(FOLDER_SEPARATOR)) { + fileName.replaceFirst(FOLDER_SEPARATOR, EMPTY_STRING); + } + return String.format(FORMAT_S_S, getGcsResDir(tenantCode), fileName); + } + + @Override + public String getResourceFileName(String fullName) { + return null; + } + + @Override + public String getFileName(ResourceType resourceType, String tenantCode, String fileName) { + if (fileName.startsWith(FOLDER_SEPARATOR)) { + fileName = fileName.replaceFirst(FOLDER_SEPARATOR, EMPTY_STRING); + } + 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()); + } + + Blob blob = gcsStorage.get(BlobId.of(bucketName, srcFilePath)); + blob.downloadTo(Paths.get(dstFilePath)); + } + + @Override + public boolean exists(String fullName) throws IOException { + return isObjectExists(fullName); + } + + @Override + public boolean delete(String filePath, boolean recursive) throws IOException { Review Comment: Hi, @zhongjiajie , thanks a lot for your kind review. Perhaps for some historical reasons, `recursive` is not used. Another interface below is called when deleting with a `childrenPathList` (also in the `S3StorageOperator` and `OssStorageOperator`). ``` @Override public boolean delete(String fullName, List<String> childrenPathList, boolean recursive) throws IOException { // append the resource fullName to the list for deletion. childrenPathList.add(fullName); boolean result = true; for (String filePath : childrenPathList) { if (!delete(filePath, recursive)) { result = false; } } return result; } ``` -- 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]
