Skip to content

Commit

Permalink
support upload file by cosn fs
Browse files Browse the repository at this point in the history
  • Loading branch information
beanwang committed Oct 17, 2023
1 parent 9dba35c commit d71abcf
Show file tree
Hide file tree
Showing 12 changed files with 688 additions and 27 deletions.
Binary file not shown.
27 changes: 26 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.qcloud</groupId>
<artifactId>cos_migrate_tool</artifactId>
<version>1.4.13</version>
<version>1.4.13.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -96,6 +96,31 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.qcloud.cos</groupId>
<artifactId>hadoop-cos</artifactId>
<version>8.2.7</version>
</dependency>

<dependency>
<groupId>com.qcloud</groupId>
<artifactId>chdfs_hadoop_plugin_network</artifactId>
<version>2.8</version>
</dependency>

<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api-bundle</artifactId>
<version>5.6.112</version>
</dependency>

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.5</version>
</dependency>

</dependencies>

<build>
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/com/qcloud/cos_migrate_tool/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,7 @@
import java.io.InputStreamReader;

import com.qcloud.cos.internal.SkipMd5CheckStrategy;
import com.qcloud.cos_migrate_tool.config.CommonConfig;
import com.qcloud.cos_migrate_tool.config.ConfigParser;
import com.qcloud.cos_migrate_tool.config.CopyBucketConfig;
import com.qcloud.cos_migrate_tool.config.CopyFromAliConfig;
import com.qcloud.cos_migrate_tool.config.CopyFromAwsConfig;
import com.qcloud.cos_migrate_tool.config.CopyFromCompetitorConfig;
import com.qcloud.cos_migrate_tool.config.CopyFromCspConfig;
import com.qcloud.cos_migrate_tool.config.CopyFromLocalConfig;
import com.qcloud.cos_migrate_tool.config.CopyFromQiniuConfig;
import com.qcloud.cos_migrate_tool.config.CopyFromUpyunConfig;
import com.qcloud.cos_migrate_tool.config.CopyFromUrllistConfig;
import com.qcloud.cos_migrate_tool.config.MigrateType;
import com.qcloud.cos_migrate_tool.config.*;
import com.qcloud.cos_migrate_tool.meta.TaskStatics;
import com.qcloud.cos_migrate_tool.task.MigrateAliTaskExecutor;
import com.qcloud.cos_migrate_tool.task.MigrateAwsTaskExecutor;
Expand All @@ -31,6 +20,7 @@
import com.qcloud.cos_migrate_tool.task.MigrateUrllistTaskExecutor;
import com.qcloud.cos_migrate_tool.task.TaskExecutor;

import com.qcloud.cos_migrate_tool.task_by_hadoop_fs.MigrateLocalToCosnTaskExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -57,6 +47,8 @@ private static TaskExecutor buildTaskExecutor(CommonConfig config) {
return new MigrateCspTaskExecutor((CopyFromCspConfig) config);
} else if (ConfigParser.instance.getMigrateType().equals(MigrateType.MIGRATE_FROM_UPYUN)) {
return new MigrateUpyunTaskExecutor((CopyFromUpyunConfig) config);
} else if (ConfigParser.instance.getMigrateType().equals(MigrateType.MIGRATE_FROM_LOCAL_TO_COSN_FS)) {
return new MigrateLocalToCosnTaskExecutor((CopyFromLocalToCosnConfig) config);
} else {
System.out.println("unknown migrate type");
}
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/com/qcloud/cos_migrate_tool/config/ConfigParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class ConfigParser {
private static final String FILE_LIST_PATH = "fileListPath";
private static final String CHECK_LOCAL_RECORD = "checkLocalRecord";

private static final String LOCAL_TO_COSN_FS_SECTION_NAME = "migrateLocalToCosnFs";

private static final String ALI_SECTION_NAME = "migrateAli";
private static final String AWS_SECTION_NAME = "migrateAws";
private static final String QINIU_SECTION_NAME = "migrateQiniu";
Expand Down Expand Up @@ -303,6 +305,14 @@ public boolean parse() {
if (!initCopyFromUpyunConfig(prefs, (CopyFromUpyunConfig) config)) {
return false;
}
} else if (migrateType.equals(MigrateType.MIGRATE_FROM_LOCAL_TO_COSN_FS)){
if (!checkMigrateLocalToCosnFsConfig(prefs)) {
return false;
}
config = new CopyFromLocalToCosnConfig();
if (!initCopyFromLocalToCosnConfig(prefs, (CopyFromLocalToCosnConfig) config)) {
return false;
}
}


Expand Down Expand Up @@ -402,6 +412,13 @@ private boolean checkMigrateLocalConfig(Preferences prefs) {
return true;
}

private boolean checkMigrateLocalToCosnFsConfig(Preferences prefs) {
if (!isKeyExist(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, LOCAL_LOCALPATH)) {
return false;
}
return true;
}

private boolean checkMigrateCopyBucketConfig(Preferences prefs) {
if (!isKeyExist(prefs, COPY_BUCKET_SECTION_NAME, COPY_SRC_REGION)) {
return false;
Expand Down Expand Up @@ -715,6 +732,69 @@ private boolean initCopyFromLocalConfig(Preferences prefs,
return true;
}

private boolean initCopyFromLocalToCosnConfig(Preferences prefs,
CopyFromLocalToCosnConfig copyLocalConfig) {
if (!initCommonConfig(prefs, copyLocalConfig)) {
return false;
}
try {

String localPathConfig = getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, LOCAL_LOCALPATH);
assert (localPathConfig != null);
copyLocalConfig.setLocalPath(localPathConfig);

String excludes = getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, LOCAL_EXECLUDE);
if (excludes != null && !excludes.trim().isEmpty()) {
copyLocalConfig.setExcludes(excludes);
} else {
excludes = getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, "excludes");
if (excludes != null && !excludes.trim().isEmpty()) {
copyLocalConfig.setExcludes(excludes);
}
}

String ignoreModifiedTimeLessThanStr =
getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, IGNORE_MODIFIED_TIME_LESS_THAN);
if (ignoreModifiedTimeLessThanStr != null
&& !ignoreModifiedTimeLessThanStr.trim().isEmpty()) {
copyLocalConfig.setIgnoreModifiedTimeLessThan(ignoreModifiedTimeLessThanStr);
}

String ignoreSuffix = getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, IGNORE_SUFFIX);
if (ignoreSuffix != null && !ignoreSuffix.trim().isEmpty()) {
copyLocalConfig.setIgnoreSuffix(ignoreSuffix);
}

String includeSuffix = getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, INCLUDE_SUFFIX);
if (includeSuffix != null && !includeSuffix.trim().isEmpty()) {
copyLocalConfig.setIncludeSuffix(includeSuffix);
}

String ignoreEmptyFile = getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, IGNORE_EMPTY_FILE);
if (ignoreEmptyFile != null && (ignoreEmptyFile.compareToIgnoreCase("on") == 0)) {
copyLocalConfig.setIgnoreEmptyFile(true);
}
String fileListMode = getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, FILE_LIST_MODE);
if (fileListMode != null && (fileListMode.compareToIgnoreCase("on") == 0)) {
copyLocalConfig.setFileListMode(true);
}
String fileListPath = getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, FILE_LIST_PATH);
if (fileListPath != null) {
copyLocalConfig.setFileListPath(fileListPath);
}

String strCheckLocal = getConfigValue(prefs, LOCAL_TO_COSN_FS_SECTION_NAME, CHECK_LOCAL_RECORD);
if (strCheckLocal != null) {
copyLocalConfig.setCheckLocalRecord(strCheckLocal);
}
} catch (Exception e) {
System.err.println(e.getMessage());
log.error(e.getMessage());
return false;
}
return true;
}

private boolean initCopyFromUrllistConfig(Preferences prefs,
CopyFromUrllistConfig copyUrllistConfig) {
if (!initCommonConfig(prefs, copyUrllistConfig)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public void setIgnoreModifiedTimeLessThan(String ignoreModifiedTimeLessThanStr)
public long getIgnoreModifiedTimeLessThan() {
return ignoreModifiedTimeLessThan;
}

public boolean isFileListMode() {
return fileListMode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com.qcloud.cos_migrate_tool.config;

import com.qcloud.cos_migrate_tool.utils.SystemUtils;

import java.io.File;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;

public class CopyFromLocalToCosnConfig extends CommonConfig{
private String localPath;
private Set<String> excludes = new HashSet<String>();
private long ignoreModifiedTimeLessThan = -1;
private Set<String> ignoreSuffixs = new HashSet<String>();
private Set<String> includeSuffixs = new HashSet<String>();
boolean ignoreEmptyFile = false;
boolean fileListMode = false;
private String fileListPath;
private boolean isCheckLocalRecord = true;

public void setIgnoreEmptyFile(boolean ignoreEmptyFile) {
this.ignoreEmptyFile = ignoreEmptyFile;
}

public void setIgnoreSuffix(String ignore) {
ignore = ignore.trim();
String[] ignoreArray = ignore.split(";");
for (String ignoreElement : ignoreArray) {
this.ignoreSuffixs.add(ignoreElement);
}
}

public void setIncludeSuffix(String include) {
include = include.trim();
String[] includeArray = include.split(";");
for (String includeElement : includeArray) {
this.includeSuffixs.add(includeElement);
}
}

public String needToMigrate(Path file, String localPath) {
if (isExcludes(localPath)) {
return "excludes";
}

for (String suffix:ignoreSuffixs) {
if(localPath.endsWith(suffix)) {
return "suffix";
}
}

if (ignoreEmptyFile) {
File localFile = new File(file.toString());
if (localFile.length() == 0) {
return "empty file";
}
}

if (includeSuffixs.isEmpty()) {
return "";
}

for (String suffix:includeSuffixs) {
if (localPath.endsWith(suffix)) {
return "";
}
}

return "do not match include suffix";
}

public String getLocalPath() {
return localPath;
}

public void setLocalPath(String localPath) throws IllegalArgumentException {
File localPathFile = new File(localPath);
if (!localPathFile.exists()) {
throw new IllegalArgumentException("local path not exist!");
}
this.localPath = SystemUtils.formatLocalPath(localPath);
}

public void setExcludes(String excludePath) throws IllegalArgumentException {
excludePath = excludePath.trim();
String[] exludePathArray = excludePath.split(";");
for (String excludePathElement : exludePathArray) {
File tempFile = new File(excludePathElement);
if (!tempFile.exists()) {
throw new IllegalArgumentException("excludePath " + excludePath + " not exist");
}
this.excludes.add(SystemUtils.formatLocalPath(tempFile.getAbsolutePath()));
}
}

public boolean isExcludes(String excludePath) {
return this.excludes.contains(excludePath);
}

public void setIgnoreModifiedTimeLessThan(String ignoreModifiedTimeLessThanStr) {
try {
long number = Long.valueOf(ignoreModifiedTimeLessThanStr);
if (number <= 0) {
throw new IllegalArgumentException(ignoreModifiedTimeLessThanStr + " is invalid, ignoreModifiedTimeLessThan must be positive");
}
this.ignoreModifiedTimeLessThan = number;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("invalid ignoreModifiedTimeLessThan");
}
}

public long getIgnoreModifiedTimeLessThan() {
return ignoreModifiedTimeLessThan;
}

public boolean isFileListMode() {
return fileListMode;
}

public void setFileListMode(boolean fileListMode) {
this.fileListMode = fileListMode;
}

public String getFileListPath() {
return fileListPath;
}

public void setFileListPath(String fileListPath) {
this.fileListPath = fileListPath;
}

public void setCheckLocalRecord(String checkLocalRecord) {
if (checkLocalRecord.compareToIgnoreCase("true") == 0) {
isCheckLocalRecord = true;
} else if (checkLocalRecord.compareToIgnoreCase("false") == 0) {
isCheckLocalRecord = false;
} else {
throw new IllegalArgumentException(checkLocalRecord + " is invalid, checkLocalRecord should be true or false");
}
}

public boolean checkLocalRecord() {
return isCheckLocalRecord;
}


public String toString() {
String strExclude = "";
if (!excludes.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String element : excludes) {
sb.append(element).append(",");
}
strExclude = sb.toString();
}


String strIgnoreSuffix = "";
if (!ignoreSuffixs.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String element : ignoreSuffixs) {
sb.append(element).append(",");
}
strIgnoreSuffix = sb.toString();
}

String strIncludeSuffix = "";
if (!includeSuffixs.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String element : includeSuffixs) {
sb.append(element).append(",");
}
strIncludeSuffix = sb.toString();
}

return super.toString()
+ ". CopyFromLocalConfig:"
+ "localPath=" + getLocalPath()
+ ",excludes=" + strExclude
+ ",ignoreModifiedTimeLessThanSeconds=" + getIgnoreModifiedTimeLessThan()
+ ",ignoreSuffix=" + strIgnoreSuffix
+ ",ignoreEmptyFile=" + ignoreEmptyFile
+ ",includeSuffix=" + strIncludeSuffix
+ ",fileListMode=" + isFileListMode()
+ ",fileListPath=" + getFileListPath()
+ ",checkLocalRecord=" + checkLocalRecord()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public enum MigrateType {
MIGRATE_FROM_URLLIST("migrateUrl"),
MIGRATE_FROM_COS_BUCKET_COPY("migrateBucketCopy"),
MIGRATE_FROM_CSP("migrateCsp"),
MIGRATE_FROM_UPYUN("migrateUpyun");
MIGRATE_FROM_UPYUN("migrateUpyun"),
MIGRATE_FROM_LOCAL_TO_COSN_FS("migrateLocalToCosnFs");

private String migrateType;

Expand Down
Loading

0 comments on commit d71abcf

Please sign in to comment.