Skip to content

Commit

Permalink
fix: rethrows BucketAlreadyExists and BucketAlreadyOwned exceptio…
Browse files Browse the repository at this point in the history
…ns as `FileSystemAlreadyExists` exception (awslabs#462)
  • Loading branch information
markjschreiber authored and stefanofornari committed May 20, 2024
1 parent 9648bee commit 80e30f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bucket-uri>");
}

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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -174,8 +177,16 @@ public FileSystem newFileSystem(final URI uri, final Map<String, ?> 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);
Expand Down

0 comments on commit 80e30f2

Please sign in to comment.