From e16ba4e5a963da193da57321e4460bc69f32848f Mon Sep 17 00:00:00 2001 From: Julian Raufelder Date: Wed, 12 May 2021 12:22:21 +0200 Subject: [PATCH] Fix creating folders in the RootFolder of the bucked in S3 Stat requests throws an IllegalStateException if key is empty string --- .../org/cryptomator/data/cloud/s3/S3Impl.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/data/src/main/java/org/cryptomator/data/cloud/s3/S3Impl.java b/data/src/main/java/org/cryptomator/data/cloud/s3/S3Impl.java index bb9003b1..c52fff9c 100644 --- a/data/src/main/java/org/cryptomator/data/cloud/s3/S3Impl.java +++ b/data/src/main/java/org/cryptomator/data/cloud/s3/S3Impl.java @@ -106,7 +106,14 @@ class S3Impl { public boolean exists(S3Node node) throws BackendException { String key = node.getKey(); try { - client().statObject(StatObjectArgs.builder().bucket(cloud.s3Bucket()).object(key).build()); + if(!(node instanceof RootS3Folder)) { + client().statObject(StatObjectArgs.builder().bucket(cloud.s3Bucket()).object(key).build()); + return true; + } else { + // stat requests throws an IllegalStateException if key is empty string + ListObjectsArgs request = ListObjectsArgs.builder().bucket(cloud.s3Bucket()).prefix(key).delimiter(DELIMITER).build(); + return client().listObjects(request).iterator().hasNext(); + } } catch (ErrorResponseException e) { if (S3CloudApiErrorCodes.NO_SUCH_KEY.getValue().equals(e.errorResponse().code())) { return false; @@ -115,7 +122,8 @@ class S3Impl { } catch (Exception ex) { handleApiError(ex, node.getPath()); } - return true; + + throw new FatalBackendException(new IllegalStateException("Exception thrown but not handled?")); } public List list(S3Folder folder) throws IOException, BackendException { @@ -221,7 +229,7 @@ class S3Impl { } } - throw new IllegalStateException(); + throw new FatalBackendException(new IllegalStateException("Exception thrown but not handled?")); } public S3File write(S3File file, DataSource data, final ProgressAware progressAware, boolean replace, long size) throws IOException, BackendException { @@ -264,7 +272,7 @@ class S3Impl { } } - throw new IllegalStateException(); + throw new FatalBackendException(new IllegalStateException("Exception thrown but not handled?")); } public void read(S3File file, OutputStream data, final ProgressAware progressAware) throws IOException, BackendException {