diff --git a/pom.xml b/pom.xml index de5d9f93..af5642cc 100644 --- a/pom.xml +++ b/pom.xml @@ -58,7 +58,7 @@ UTF-8 1.7 1.7 - 3.3.3 + 2.8.5 5.6.137.2 24.1.1-jre 3.1 diff --git a/src/main/java/org/apache/hadoop/fs/CosNFileSystem.java b/src/main/java/org/apache/hadoop/fs/CosNFileSystem.java index b06d6f03..e10d6872 100644 --- a/src/main/java/org/apache/hadoop/fs/CosNFileSystem.java +++ b/src/main/java/org/apache/hadoop/fs/CosNFileSystem.java @@ -1527,28 +1527,19 @@ private Path makeAbsolute(Path path) { return new Path(workingDir, path); } - // key and path relate - public static String pathToKey(Path path) { - if (path.toUri().getScheme() != null && path.toUri().getPath().isEmpty()) { - // allow uris without trailing slash after bucket to refer to root, - // like cosn://mybucket - return ""; - } - if (!path.isAbsolute()) { - throw new IllegalArgumentException("Path must be absolute: " + path); - } - String ret = path.toUri().getPath(); - if (ret.endsWith("/") && (ret.indexOf("/") != ret.length() - 1)) { - ret = ret.substring(0, ret.length() - 1); - } - return ret; + /** + * 将一个 hadoop 的 path 路径转换为 COS 的 key 路径。 + * 由于每个方法都会调用这个方法,因此这里调用了 checkPath 检查传入路径是否归属于当前的文件系统 URI。 + * + * @param path hadoop 的 path 路径,原则上应是一个绝对路径。 + * @return 传入的 hadoop 的 path 对应的 COS 的 key 路径。 + */ + public String pathToKey(Path path) { + super.checkPath(path); + return CosNUtils.pathToKey(path); } - public static Path keyToPath(String key) { - if (!key.startsWith(PATH_DELIMITER)) { - return new Path("/" + key); - } else { - return new Path(key); - } + public Path keyToPath(String key) { + return CosNUtils.keyToPath(key, PATH_DELIMITER); } } diff --git a/src/main/java/org/apache/hadoop/fs/CosNUtils.java b/src/main/java/org/apache/hadoop/fs/CosNUtils.java index 3d7c6228..a1545c0a 100644 --- a/src/main/java/org/apache/hadoop/fs/CosNUtils.java +++ b/src/main/java/org/apache/hadoop/fs/CosNUtils.java @@ -309,4 +309,27 @@ public static Configuration propagateBucketOptions(Configuration source, String return dest; } + public static String pathToKey(Path path) { + if (path.toUri().getScheme() != null && path.toUri().getPath().isEmpty()) { + // allow uris without trailing slash after bucket to refer to root, + // like cosn://mybucket + return ""; + } + if (!path.isAbsolute()) { + throw new IllegalArgumentException("Path must be absolute: " + path); + } + String ret = path.toUri().getPath(); + if (ret.endsWith("/") && (ret.indexOf("/") != ret.length() - 1)) { + ret = ret.substring(0, ret.length() - 1); + } + return ret; + } + + public static Path keyToPath(String key, String pathDelimiter) { + if (!key.startsWith(pathDelimiter)) { + return new Path("/" + key); + } else { + return new Path(key); + } + } } diff --git a/src/main/java/org/apache/hadoop/fs/RangerCredentialsClient.java b/src/main/java/org/apache/hadoop/fs/RangerCredentialsClient.java index 0ac19ed1..acd3e009 100644 --- a/src/main/java/org/apache/hadoop/fs/RangerCredentialsClient.java +++ b/src/main/java/org/apache/hadoop/fs/RangerCredentialsClient.java @@ -68,7 +68,7 @@ public void doCheckPermission(Path f, RangerAccessType rangerAccessType, String } Path absolutePath = makeAbsolute(f, workingDirectory); - String allowKey = CosNFileSystem.pathToKey(absolutePath); + String allowKey = CosNUtils.pathToKey(absolutePath); if (allowKey.startsWith("/")) { allowKey = allowKey.substring(1); }