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


##########
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:
   Good catch — dropped the claim. The javadoc now states the deliberate 
difference: flink-s3-fs-hadoop answers the root locally without a request, 
whereas here a missing bucket surfaces as FileNotFoundException and an access 
denial as IOException.



##########
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:
   Done — dropped the dedicated NoSuchBucketException catch (and its import); 
the statusCode() == 404 branch now handles it, with a comment noting the 
relationship.



##########
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:
   Limited the check to the bucket root as suggested — non-root prefixes keep 
the pre-existing empty-stream behavior, and added 
testOpenDirectoryPrefixReturnsEmptyStream pinning that.



##########
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:
   You're right (verified against hadoop-aws 3.4.3: DeleteOperation logs "S3A: 
Cannot delete the root directory" and returns false). Changed to log a warning 
and return false instead of throwing; the test now asserts false and that 
bucket contents survive.



##########
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:
   Fixed — missingFs is now closed via closeAsync().get() in a finally block.



-- 
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