Browse Source

HDDS-210. Make "-file" argument optional for ozone getKey command. Contributed by Lokesh Jain.

Xiaoyu Yao 7 years ago
parent
commit
103f2eeb57

+ 12 - 0
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/ozShell/TestOzoneShell.java

@@ -705,6 +705,18 @@ public class TestOzoneShell {
       randFile.read(dataBytes);
     }
     assertEquals(dataStr, DFSUtil.bytes2String(dataBytes));
+
+    tmpPath = baseDir.getAbsolutePath() + File.separatorChar + keyName;
+    args = new String[] {"-getKey",
+        url + "/" + volumeName + "/" + bucketName + "/" + keyName, "-file",
+        baseDir.getAbsolutePath()};
+    assertEquals(0, ToolRunner.run(shell, args));
+
+    dataBytes = new byte[dataStr.length()];
+    try (FileInputStream randFile = new FileInputStream(new File(tmpPath))) {
+      randFile.read(dataBytes);
+    }
+    assertEquals(dataStr, DFSUtil.bytes2String(dataBytes));
   }
 
   @Test

+ 6 - 3
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/GetKeyHandler.java

@@ -98,11 +98,14 @@ public class GetKeyHandler extends Handler {
     Path dataFilePath = Paths.get(fileName);
     File dataFile = new File(fileName);
 
+    if (dataFile.exists() && dataFile.isDirectory()) {
+      dataFile = new File(fileName, keyName);
+    }
 
     if (dataFile.exists()) {
-      throw new OzoneClientException(fileName +
-                                         "exists. Download will overwrite an " +
-                                         "existing file. Aborting.");
+      throw new OzoneClientException(
+          fileName + "exists. Download will overwrite an "
+              + "existing file. Aborting.");
     }
 
     OzoneVolume vol = client.getObjectStore().getVolume(volumeName);