From 80e30f297c5ae39b176d4ac49cb531d667121ed1 Mon Sep 17 00:00:00 2001 From: Mark Schreiber Date: Fri, 17 May 2024 15:13:07 -0400 Subject: [PATCH] fix: rethrows `BucketAlreadyExists` and `BucketAlreadyOwned` exceptions as `FileSystemAlreadyExists` exception (#462) --- .../amazon/nio/spi/examples/CreateBucket.java | 15 ++++++++++++++- .../amazon/nio/spi/s3/S3FileSystemProvider.java | 13 ++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/examples/java/software/amazon/nio/spi/examples/CreateBucket.java b/src/examples/java/software/amazon/nio/spi/examples/CreateBucket.java index 9ac6e69c..3a81228a 100644 --- a/src/examples/java/software/amazon/nio/spi/examples/CreateBucket.java +++ b/src/examples/java/software/amazon/nio/spi/examples/CreateBucket.java @@ -7,14 +7,27 @@ import java.io.IOException; import java.net.URI; +import java.nio.file.FileSystemAlreadyExistsException; import java.nio.file.FileSystems; import java.util.Map; public class CreateBucket { public static void main(String[] args) throws IOException { + + if (args.length != 1) { + throw new IllegalArgumentException("Usage: CreateBucket "); + } + + if (!args[0].startsWith("s3://")) { + throw new IllegalArgumentException("Bucket URI must start with s3://"); + } + + System.out.println("Creating bucket " + args[0]); try (var fs = FileSystems.newFileSystem(URI.create(args[0]), - Map.of("locationConstraint", "us-east-1"))) { + Map.of("locationConstraint", "us-west-2"))) { System.out.println(fs.toString()); + } catch (FileSystemAlreadyExistsException e) { + System.err.printf("Bucket already exists: %s\n", e.getMessage()); } } } diff --git a/src/main/java/software/amazon/nio/spi/s3/S3FileSystemProvider.java b/src/main/java/software/amazon/nio/spi/s3/S3FileSystemProvider.java index 04f4d655..06679613 100644 --- a/src/main/java/software/amazon/nio/spi/s3/S3FileSystemProvider.java +++ b/src/main/java/software/amazon/nio/spi/s3/S3FileSystemProvider.java @@ -21,6 +21,7 @@ import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileStore; import java.nio.file.FileSystem; +import java.nio.file.FileSystemAlreadyExistsException; import java.nio.file.FileSystemNotFoundException; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -58,6 +59,8 @@ import software.amazon.awssdk.core.exception.SdkException; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3AsyncClient; +import software.amazon.awssdk.services.s3.model.BucketAlreadyExistsException; +import software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException; import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm; import software.amazon.awssdk.services.s3.model.CopyObjectRequest; import software.amazon.awssdk.services.s3.model.Delete; @@ -174,8 +177,16 @@ public FileSystem newFileSystem(final URI uri, final Map env) throws logger.debug("Create bucket response {}", createBucketResponse.toString()); } catch (ExecutionException e) { - throw new IOException(e.getMessage(), e.getCause()); + if (e.getCause() instanceof BucketAlreadyOwnedByYouException || + e.getCause() instanceof BucketAlreadyExistsException) { + throw new FileSystemAlreadyExistsException(e.getCause().getMessage()); + } else { + throw new IOException(e.getMessage(), e.getCause()); + } } catch (InterruptedException | TimeoutException | SdkException e) { + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } throw new IOException(e.getMessage(), e); } return getFileSystem(uri, true);