Closed mslinn closed 10 years ago
The problem actually is caused by S3FileObject not determining if the parent is a child directory of ROOT or ROOT itself. In the case the parent is ROOT then the NameScope should be NameScope.FILE_SYSTEM otherwise it is NameScope.CHILD. I updated the S3FileObject and created a unit test for listChildrenRoot , see the patch below.
diff --git a/src/main/java/com/intridea/io/vfs/provider/s3/S3FileObject.java b/src/main/java/com/intridea/io/vfs/provider/s3/S3FileObject.java index 4f20f53..a77bbff 100644 --- a/src/main/java/com/intridea/io/vfs/provider/s3/S3FileObject.java +++ b/src/main/java/com/intridea/io/vfs/provider/s3/S3FileObject.java @@ -293,8 +293,8 @@ for (String commonPrefix : commonPrefixes) { // strip path from name (leave only base name) final String stripPath = commonPrefix.substring(path.length()); - FileObject childObject = resolveFile(stripPath, NameScope.CHILD); - if (childObject instanceof S3FileObject) { + FileObject childObject = resolveFile(stripPath, stripPath.equals("/")?NameScope.FILE_SYSTEM:NameScope.CHILD); + if (childObject instanceof S3FileObject && !stripPath.equals("/")) { S3FileObject s3FileObject = (S3FileObject) childObject; resolvedChildren.add(s3FileObject); } diff --git a/src/test/java/com/intridea/io/vfs/provider/s3/S3ProviderTest.java b/src/test/java/com/intridea/io/vfs/provider/s3/S3ProviderTest.java index 7bc7c47..54aa4e0 100644 --- a/src/test/java/com/intridea/io/vfs/provider/s3/S3ProviderTest.java +++ b/src/test/java/com/intridea/io/vfs/provider/s3/S3ProviderTest.java @@ -325,6 +325,13 @@ FileObject[] children = baseDir.getChildren(); assertEquals(children.length, 5); } + + @Test(dependsOnMethods={"createFileOk", "createDirOk","uploadBigFile"}) + public void listChildrenRoot() throws FileSystemException { + FileObject root = fsManager.resolveFile("s3://" + bucketName + "/"); + FileObject[] children = root.getChildren(); + assertEquals(children.length, 2); + } @Test(dependsOnMethods={"createDirOk"}) public void findFiles() throws FileSystemException {
I have a Scala/sbt test project at https://github.com/mslinn/vfs-s3Test. The tests show a few issues:
getChildren
would be the right method to call, but perhaps I misunderstand the API. This problem causes the cleanDirectory test to loop foreverorg.apache.commons.vfs2.FileSystemException: Could not find files in "s3://mslinntest/testDir".
This may be a related problem.@mslinn to be fair I was not able to reproduce your issue. Maybe you took old code or 2.1.x branch, please update to latest version and let me know does it fix everything or not.
And I don't see any problems with including code from @cuzz22000 into list of tests. I did a little refactoring for it so here you go - https://github.com/abashev/vfs-s3/commit/133a4844a0662291bb084b8ca507b12354c2565a
AWS CLI will not list the contents of a directory unless the path ends in a slash:
Seems the Java library works the same way, but vfs-s3 removes the trailing slash so
getChildren()
does not return the expected value:Output is:
Should I not use
getChildren()
because there is a better way of listing an S3 directory?Here is my project with both Scala and Java code that demonstrates this problem.