MartijnVisser commented on code in PR #29420:
URL: https://github.com/apache/flink/pull/29420#discussion_r4785210237


##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/NativeS3FileSystem.java:
##########
@@ -274,6 +282,29 @@ public FileStatus getFileStatus(Path path) throws 
IOException {
         }
     }
 
+    /**
+     * Returns the {@link FileStatus} of a bucket-root path (an empty object 
key). Since S3 has no
+     * object to Head for the bucket root, the bucket is verified via 
HeadBucket (surfacing
+     * missing-bucket or permission errors) and reported as a directory, 
matching the behavior of
+     * flink-s3-fs-hadoop.

Review Comment:
   S3A answers the root without a request, so a missing bucket or credentials 
limited to a prefix give `exists()` true there. Here it is false and a 403, 
which I prefer, so please drop "matching flink-s3-fs-hadoop".



##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/NativeS3FileSystem.java:
##########
@@ -274,6 +282,29 @@ public FileStatus getFileStatus(Path path) throws 
IOException {
         }
     }
 
+    /**
+     * Returns the {@link FileStatus} of a bucket-root path (an empty object 
key). Since S3 has no
+     * object to Head for the bucket root, the bucket is verified via 
HeadBucket (surfacing
+     * missing-bucket or permission errors) and reported as a directory, 
matching the behavior of
+     * flink-s3-fs-hadoop.
+     */
+    private FileStatus getBucketRootStatus(S3Client s3Client, Path path) 
throws IOException {
+        LOG.debug("Getting file status for bucket root: s3://{}", bucketName);
+        try {
+            
s3Client.headBucket(HeadBucketRequest.builder().bucket(bucketName).build());
+        } catch (NoSuchBucketException e) {

Review Comment:
   `NoSuchBucketException` is an `S3Exception` with status 404, so the status 
check below already covers it and this catch can go.



##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/NativeS3FileSystem.java:
##########
@@ -306,17 +337,24 @@ public FSDataInputStream open(Path path, int bufferSize) 
throws IOException {
         checkNotClosed();
         final String key = S3UriUtils.extractKey(path);
         final S3Client s3Client = clientProvider.getS3Client();
-        final long fileSize = getFileStatus(path).getLen();
-        return new NativeS3InputStream(s3Client, bucketName, key, fileSize, 
bufferSize);
+        return open(path, key, s3Client, bufferSize);
     }
 
     @Override
     public FSDataInputStream open(Path path) throws IOException {
         checkNotClosed();
         final String key = S3UriUtils.extractKey(path);
         final S3Client s3Client = clientProvider.getS3Client();
-        final long fileSize = getFileStatus(path).getLen();
-        return new NativeS3InputStream(s3Client, bucketName, key, fileSize, 
readBufferSize);
+        return open(path, key, s3Client, readBufferSize);
+    }
+
+    private FSDataInputStream open(Path path, String key, S3Client s3Client, 
int bufferSize)
+            throws IOException {
+        final FileStatus status = getFileStatus(path);
+        if (status.isDir()) {

Review Comment:
   This changes `open()` for every directory: on master, opening a prefix with 
children returns an empty stream. Please add a test for that case; limiting 
this check to the root still passes the new tests.



##########
flink-filesystems/flink-s3-fs-native/src/test/java/org/apache/flink/fs/s3native/NativeS3FileSystemITCase.java:
##########
@@ -103,6 +107,56 @@ void testListRenameDelete() throws Exception {
         assertThat(fs.exists(renamed)).isFalse();
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = {"", "/"})
+    void testBucketRootExistsAsDirectory(String rootSuffix) throws Exception {
+        final Path root = new Path(bucketUri + rootSuffix);
+
+        assertThat(fs.exists(root)).isTrue();
+        final FileStatus status = fs.getFileStatus(root);
+        assertThat(status.isDir()).isTrue();
+        assertThat(status.getLen()).isZero();
+        assertThat(status.getPath()).isEqualTo(root);
+    }
+
+    @Test
+    void testBucketRootCannotBeOpened() throws Exception {
+        write(path("openroot/" + UUID.randomUUID() + ".txt"), 
"x".getBytes(StandardCharsets.UTF_8));
+
+        assertThatThrownBy(() -> fs.open(new Path(bucketUri)))
+                .isInstanceOf(FileNotFoundException.class)
+                .hasMessageContaining("directory");
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    void testBucketRootCannotBeDeleted(boolean recursive) throws Exception {
+        final String dir = "deleteroot-" + UUID.randomUUID();
+        write(path(dir + "/" + UUID.randomUUID() + ".txt"), 
"x".getBytes(StandardCharsets.UTF_8));
+
+        assertThatThrownBy(() -> fs.delete(new Path(bucketUri), recursive))
+                .isInstanceOf(IOException.class)
+                .hasMessageContaining("root");
+        assertThat(fs.listStatus(path(dir))).hasSize(1);
+    }
+
+    @Test
+    void testMissingBucketRootIsReportedAsNotFound() throws Exception {
+        final Configuration config = new Configuration();
+        container().setS3ConfigOptions(config);
+
+        final NativeS3FileSystemFactory factory = new 
NativeS3FileSystemFactory();
+        factory.configure(config);
+
+        final String missingBucketUri =
+                "s3://missing-bucket-" + 
UUID.randomUUID().toString().toLowerCase(Locale.ROOT);
+        final FileSystem missingFs = 
factory.create(URI.create(missingBucketUri + "/"));

Review Comment:
   `missingFs` is never closed, so its clients and upload pool stay alive until 
the JVM exits.



##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/NativeS3FileSystem.java:
##########
@@ -387,6 +425,12 @@ public boolean delete(Path path, boolean recursive) throws 
IOException {
         final String key = S3UriUtils.extractKey(path);
         final S3Client s3Client = clientProvider.getS3Client();
 
+        if (key.isEmpty()) {
+            // The bucket root resolves to a directory status; deleting it 
would either wipe the
+            // whole bucket or silently no-op. Match S3A and refuse.

Review Comment:
   S3A refuses a root delete by returning false (`DeleteOperation` logs and 
returns), not by throwing. Is the exception intended?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to