Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rethrows BucketAlreadyExists and BucketAlreadyOwned #462

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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