samredai commented on code in PR #5332:
URL: https://github.com/apache/iceberg/pull/5332#discussion_r931752752


##########
python/pyiceberg/io/s3fs.py:
##########
@@ -0,0 +1,235 @@
+# 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.
+"""FileIO implementation for reading and writing table files that uses s3fs"""
+
+from typing import Optional, Union
+
+from s3fs import S3FileSystem
+
+from pyiceberg.io.base import (
+    FileIO,
+    InputFile,
+    InputStream,
+    OutputFile,
+    OutputStream,
+)
+
+
+class S3fsInputStream(InputStream):
+    """A seekable wrapper for reading an S3 Object that abides by the 
InputStream protocol
+
+    Args:
+        s3_object(s3fs.core.S3File): An s3 object
+    """
+
+    def __init__(self, s3_object):
+        self._s3_object = s3_object
+
+    def read(self, size: int = -1) -> bytes:
+        """Read the byte content of the s3 object
+
+        Args:
+            size (int, optional): The number of bytes to read. Defaults to -1 
which reads the entire file.
+
+        Returns:
+            bytes: The byte content of the file
+        """
+        return self._s3_object.read(length=size)
+
+    def seek(self, offset: int, whence: int = 0) -> int:
+        return self._s3_object.seek(loc=offset, whence=whence)
+
+    def tell(self) -> int:
+        return self._s3_object.tell()
+
+    def closed(self) -> bool:
+        return self._s3_object.closed
+
+    def close(self) -> None:
+        self._s3_object.close()
+
+
+class S3fsOutputStream(OutputStream):
+    """A wrapper for writing an S3 Object that abides by the OutputStream 
protocol
+
+    Args:
+        s3_object(s3fs.core.S3FileSystem): An s3 object
+    """
+
+    def __init__(self, s3_object):
+        self._s3_object = s3_object
+
+    def write(self, b: bytes) -> int:
+        """Write to the S3 Object
+
+        Args:
+            b(bytes): The bytes to write to the S3 Object
+
+        Returns:
+            int: The number of bytes written
+
+        Raises:
+            ValueError: When the file is closed
+        """
+        return self._s3_object.write(b)
+
+    def closed(self) -> bool:
+        """Returns whether the stream is closed or not"""
+        return self._s3_object.closed
+
+    def close(self) -> None:
+        """Closes the stream and uploads the bytes to S3"""
+        self._s3_object.close()
+
+
+class S3fsInputFile(InputFile):
+    """An input file implementation for the S3fsFileIO
+
+    Args:
+        location(str): An S3 URI
+
+    Attributes:
+        location(str): An S3 URI
+    """
+
+    def __init__(self, location: str, s3):
+        self._s3 = s3
+        super().__init__(location=location)
+
+    def __len__(self) -> int:
+        """Returns the total length of the file, in bytes"""
+        object_info = self._s3.info(self.location)
+        if object_info.get("Size"):  # s3fs versions seem inconsistent on the 
case used for size

Review Comment:
   I like that but you also reminded me that I could just do `return 
object_info.get("Size") or object_info.get("size")` and let python figure out 
which one is not `None` :)



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to