This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
new 4fc3abf987 fix(S3): fix browsing the root of a bucket and directory
names
4fc3abf987 is described below
commit 4fc3abf9871b4517d8ab1d25e552b6d6bcd94d79
Author: qbialota <[email protected]>
AuthorDate: Fri Nov 13 11:53:04 2026 +0100
fix(S3): fix browsing the root of a bucket and directory names
* `FileService.checkAccess(…)` reported the root of a bucket as
non-existent when the path
was built from an URI (e.g. "s3://host:port/bucket"): the `Bucket`
metadata created from
the name only has no creation date. The bucket existence is now asked to
the server with
a `HeadBucket` request.
* `PathIterator` failed with a `NullPointerException` when listing the root
of a bucket,
because the directory key is null in that case.
* `KeyPath.toString()` on a relative directory path, for example the file
name of a
directory, contained the server part ("S3://accessKey@host:port/name")
instead of the
name only.
---
.../org/apache/sis/cloud/aws/s3/FileService.java | 20 ++-
.../main/org/apache/sis/cloud/aws/s3/KeyPath.java | 4 +-
.../org/apache/sis/cloud/aws/s3/PathIterator.java | 3 +-
.../org/apache/sis/cloud/aws/s3/KeyPathTest.java | 17 +++
.../apache/sis/cloud/aws/s3/PathIteratorTest.java | 141 +++++++++++++++++++++
5 files changed, 181 insertions(+), 4 deletions(-)
diff --git
a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/FileService.java
b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/FileService.java
index 27baabdd41..e65409d053 100644
---
a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/FileService.java
+++
b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/FileService.java
@@ -54,6 +54,7 @@ import software.amazon.awssdk.services.s3.model.S3Object;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
+import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
import org.apache.sis.util.CharSequences;
import org.apache.sis.util.ArgumentChecks;
import org.apache.sis.util.resources.Errors;
@@ -673,7 +674,8 @@ public class FileService extends FileSystemProvider {
final boolean exists;
if (kp.isDirectory) {
final Bucket bucket = kp.bucket();
- exists = (bucket != null) && (bucket.creationDate() != null);
+ // A bucket built from its name only (e.g. from an URI) has no
creation date: ask the server.
+ exists = (bucket != null) && (bucket.creationDate() != null ||
bucketExists(kp));
} else {
final S3Object metadata = kp.metadata();
exists = (metadata != null) && (metadata.size() != null);
@@ -694,6 +696,22 @@ public class FileService extends FileSystemProvider {
}
}
+ /**
+ * Returns whether the bucket of the given path exists on the server.
+ *
+ * @param kp an absolute path.
+ * @return {@code false} if the server reports that the bucket does not
exist.
+ * @throws SdkException if the request failed for another reason (e.g.
access denied).
+ */
+ private static boolean bucketExists(final KeyPath kp) {
+ try {
+
kp.fs.client().headBucket(HeadBucketRequest.builder().bucket(kp.bucket).build());
+ return true;
+ } catch (NoSuchBucketException e) {
+ return false;
+ }
+ }
+
/**
* Returns a read-only or updatable view of a set of file attributes.
*
diff --git
a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/KeyPath.java
b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/KeyPath.java
index 5ce7070213..0306932650 100644
---
a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/KeyPath.java
+++
b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/KeyPath.java
@@ -744,8 +744,8 @@ search: if (key != null) {
*/
@Override
public String toString() {
- if (bucket == null && !isDirectory) {
- return key;
+ if (bucket == null && (key != null || !isDirectory)) {
+ return key; // Relative path (e.g. a file name), possibly a
directory: no "server part".
}
final var sb = fs.server.toString(bucket);
if (key != null) {
diff --git
a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/PathIterator.java
b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/PathIterator.java
index f1b8c15d32..6828cfdf7f 100644
---
a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/PathIterator.java
+++
b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/PathIterator.java
@@ -187,7 +187,8 @@ verify: do if (directories == null || !nextDirectory())
{
int last = path.length();
do if ((last -= separatorLength) < 0) return false;
while (path.startsWith(separator, last));
- if (path.regionMatches(0, directory.key, 0, last +
separatorLength)) {
+ // `directory.key` is null when listing the root of a bucket (no
object can have the name of the bucket).
+ if (directory.key != null && path.regionMatches(0, directory.key,
0, last + separatorLength)) {
return false;
}
}
diff --git
a/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/KeyPathTest.java
b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/KeyPathTest.java
index d7b719d8fa..4b5c6f7a17 100644
---
a/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/KeyPathTest.java
+++
b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/KeyPathTest.java
@@ -128,6 +128,23 @@ public final class KeyPathTest extends TestCase {
assertNull(p.key);
}
+ /**
+ * Tests {@link KeyPath#toString()} on the name of a directory.
+ * It used to contain the server part, for example {@code
"S3://testhost:8581/second"}.
+ *
+ * @throws URISyntaxException if an error occurred while creating the
<abbr>URI</abbr> for the self-hosted S3.
+ */
+ @Test
+ public void testDirectoryFileName() throws URISyntaxException {
+ for (final boolean selfHosted : new boolean[] {false, true}) {
+ createPaths(selfHosted);
+ assertNotNull(absolute.getRoot());
+ final var directory = new KeyPath((KeyPath) absolute.getRoot(),
"first/second", true);
+ assertEquals("second", directory.getFileName().toString());
+ assertEquals("second", new KeyPath(fs, "second", true).toString());
+ }
+ }
+
/**
* Tests {@link KeyPath#getParent()}.
*
diff --git
a/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/PathIteratorTest.java
b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/PathIteratorTest.java
new file mode 100644
index 0000000000..0b7b2c1c25
--- /dev/null
+++
b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/PathIteratorTest.java
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.cloud.aws.s3;
+
+import java.lang.reflect.Field;
+import java.net.URISyntaxException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import software.amazon.awssdk.services.s3.S3Client;
+import software.amazon.awssdk.services.s3.model.Bucket;
+import software.amazon.awssdk.services.s3.model.CommonPrefix;
+import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
+import software.amazon.awssdk.services.s3.model.HeadBucketResponse;
+import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
+import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
+import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
+import software.amazon.awssdk.services.s3.model.S3Object;
+
+// Test dependencies
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+import org.apache.sis.test.TestCase;
+
+
+/**
+ * Tests {@link PathIterator}, and {@link FileService#checkAccess(Path,
java.nio.file.AccessMode...)} on a bucket root,
+ * using a fake S3 client.
+ *
+ * @author Quentin Bialota (Geomatys)
+ */
+@SuppressWarnings("exports")
+public final class PathIteratorTest extends TestCase {
+ /**
+ * Creates a new test case.
+ */
+ public PathIteratorTest() {
+ }
+
+ /**
+ * A fake S3 server with the given existing buckets. Each bucket contains
+ * a "common/" pseudo-directory and a "data.nc" object at its root.
+ */
+ private static final class FakeClient implements S3Client {
+ private final Set<String> buckets;
+
+ FakeClient(final String... buckets) {
+ this.buckets = Set.of(buckets);
+ }
+
+ @Override public String serviceName() {return "s3";}
+ @Override public void close() {}
+
+ @Override
+ public HeadBucketResponse headBucket(final HeadBucketRequest request) {
+ if (!buckets.contains(request.bucket())) {
+ throw
NoSuchBucketException.builder().message(request.bucket()).build();
+ }
+ return HeadBucketResponse.builder().build();
+ }
+
+ @Override
+ public ListObjectsV2Response listObjectsV2(final ListObjectsV2Request
request) {
+ assertNull(request.prefix(), "Listing the root of a bucket should
not use a prefix.");
+ return ListObjectsV2Response.builder()
+
.commonPrefixes(CommonPrefix.builder().prefix("common/").build())
+
.contents(S3Object.builder().key("data.nc").size(10L).build())
+ .isTruncated(false)
+ .build();
+ }
+ }
+
+ /**
+ * Creates a self-hosted file system backed by a fake client.
+ */
+ static ClientFileSystem createFileSystem(final String... buckets) throws
URISyntaxException, ReflectiveOperationException {
+ final ClientFileSystem fs = ClientFileSystemTest.create(true);
+ final Field client = ClientFileSystem.class.getDeclaredField("client");
+ client.setAccessible(true);
+ client.set(fs, new FakeClient(buckets));
+ return fs;
+ }
+
+ /**
+ * Returns the root of the given bucket as built from an URI, i.e. without
bucket metadata.
+ */
+ static KeyPath bucketRoot(final ClientFileSystem fs, final String bucket) {
+ return new KeyPath(fs, bucket, new String[0], true);
+ }
+
+ /**
+ * Lists the root of a bucket. Used to fail with a {@link
NullPointerException}
+ * because the root has no key. Also checks that directory names are only
names.
+ *
+ * @throws Exception if an error occurred while creating the file system
or listing the bucket.
+ */
+ @Test
+ public void testListBucketRoot() throws Exception {
+ final ClientFileSystem fs = createFileSystem("the-bucket");
+ final List<String> names = new ArrayList<>();
+ try (DirectoryStream<Path> stream =
fs.provider().newDirectoryStream(bucketRoot(fs, "the-bucket"), (p) -> true)) {
+ for (final Path child : stream) {
+ names.add(child.getFileName().toString());
+ }
+ }
+ assertEquals(List.of("common", "data.nc"), names);
+ }
+
+ /**
+ * Tests {@code checkAccess} on the root of a bucket built from an URI (no
creation date).
+ *
+ * @throws Exception if an error occurred while creating the file system.
+ */
+ @Test
+ public void testBucketRootExists() throws Exception {
+ final ClientFileSystem fs = createFileSystem("the-bucket");
+ final FileService service = (FileService) fs.provider();
+ assertDoesNotThrow(() -> service.checkAccess(bucketRoot(fs,
"the-bucket")));
+ assertThrows(java.nio.file.NoSuchFileException.class,
+ () -> service.checkAccess(bucketRoot(fs, "missing-bucket")));
+ // A bucket listed by the server has a creation date: no request
needed.
+ final KeyPath listed = new KeyPath(fs,
Bucket.builder().name("other").creationDate(java.time.Instant.EPOCH).build());
+ assertDoesNotThrow(() -> service.checkAccess(listed));
+ }
+}