Skip to content

Commit

Permalink
use standard nio patterns (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
markjschreiber authored Oct 26, 2023
1 parent 1808081 commit 03758c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/examples/java/software/amazon/nio/spi/examples/ListPrefix.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package software.amazon.nio.spi.examples;

import software.amazon.nio.spi.s3.S3FileSystem;
import software.amazon.nio.spi.s3.S3Path;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Collections;

import static java.util.Collections.*;

public class ListPrefix {
public static void main(String[] args) throws IOException {
Expand All @@ -15,8 +19,12 @@ public static void main(String[] args) throws IOException {
}

String prefix = args[0];
try (final FileSystem fileSystem = FileSystems.newFileSystem(URI.create(prefix), Collections.EMPTY_MAP)) {
try (final FileSystem fileSystem = FileSystems.newFileSystem(URI.create(prefix), emptyMap())) {
Path s3Path = fileSystem.getPath(prefix);

assert fileSystem.getClass().getName().contains("S3FileSystem");
assert s3Path.getClass().getName().contains("S3Path");

fileSystem.provider()
.newDirectoryStream(s3Path, item -> true)
.forEach(path -> System.out.println(path.getFileName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.stream.Stream;

public class WalkFromRoot {

Expand All @@ -19,14 +20,25 @@ public class WalkFromRoot {
* @throws IOException if a communication problem happens with the S3 service.
*/
public static void main(String[] args) throws IOException {

if (args.length < 1){
System.err.println("Provide a bucket name to walk");
System.exit(1);
}

String bucketName = args[0];
if (!bucketName.startsWith("s3:") && !bucketName.startsWith("s3x:")) {
bucketName = "s3://" + bucketName;
}
final FileSystem s3 = FileSystems.newFileSystem(URI.create(bucketName), Collections.EMPTY_MAP);
try (FileSystem s3 = FileSystems.newFileSystem(URI.create(bucketName), Collections.emptyMap())) {

assert s3.getClass().getName().contains("S3FileSystem");

for (Path rootDir : s3.getRootDirectories()) {
Files.walk(rootDir).forEach(System.out::println);
for (Path rootDir : s3.getRootDirectories()) {
try (Stream<Path> pathStream = Files.walk(rootDir)) {
pathStream.forEach(System.out::println);
}
}
}
}
}

0 comments on commit 03758c0

Please sign in to comment.