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


##########
python/src/iceberg/io/boto.py:
##########
@@ -0,0 +1,291 @@
+# 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 boto3"""
+
+from io import SEEK_CUR, SEEK_END, SEEK_SET
+from typing import Optional, Union
+from urllib.parse import urlparse
+
+from boto3 import Session
+from botocore.exceptions import ClientError
+
+from iceberg.io.base import FileIO, InputFile, InputStream, OutputFile, 
OutputStream
+
+
+class BotoInputStream(InputStream):
+    """A seekable wrapper for reading an S3 Object that abides by the 
InputStream protocol
+
+    Args:
+        s3_object(boto3.resources.factory.s3.Object): An s3 object
+    """
+
+    def __init__(self, s3_object):
+        self._s3_object = s3_object
+        self._position = 0
+
+    def read(self, n: int = -1) -> bytes:
+        """Read the byte content of the s3 object
+
+        This uses the `Range` argument when reading the S3 Object that allows 
setting a range of bytes to the headers of the request to S3.
+
+        Args:
+            n (int, optional): The number of bytes to read. Defaults to -1 
which reads the entire file.
+
+        Returns:
+            bytes: The byte content of the file
+        """
+        if n < 0:  # Read the entire file from the current position
+            range_header = f"bytes={self._position}-"
+            self.seek(offset=0, whence=2)
+        else:
+            position_new = self._position + n
+
+            if (
+                position_new >= self._s3_object.content_length
+            ):  # If more bytes are requested than exists, just read the 
entire file from the current position
+                return self.read(n=-1)
+
+            range_header = f"bytes={self._position}-{position_new -1}"
+            self.seek(offset=n, whence=SEEK_CUR)
+
+        return self._s3_object.get(Range=range_header)["Body"].read()
+
+    def seek(self, offset: int, whence: int = 0) -> int:
+        if whence not in {SEEK_SET, SEEK_CUR, SEEK_END}:
+            raise ValueError(f"Cannot seek to position {offset}, invalid 
whence: {whence}")
+
+        if whence == SEEK_SET:
+            self._position = offset
+        elif whence == SEEK_CUR:
+            self._position = self._position + offset
+        elif whence == SEEK_END:
+            self._position = self._s3_object.content_length - offset
+
+        return self._position
+
+    def tell(self) -> int:
+        return self._position
+
+    def closed(self) -> bool:
+        return False
+
+    def close(self) -> None:
+        pass
+
+
+class BotoOutputStream(OutputStream):
+    """A wrapper for writing an S3 Object that abides by the OutputStream 
protocol
+
+    Args:
+    s3_object(boto3.resources.factory.s3.Object): An s3 object
+    """
+
+    def __init__(self, s3_object):
+        self._s3_object = s3_object
+
+    def write(self, b: bytes) -> None:
+        """Write to the S3 Object
+
+        Args:
+            b(bytes): The bytes to write to the S3 Object
+        """
+        self._s3_object.put(Body=b)

Review Comment:
   Updated to just append to `self._content` and then only uploads once 
`.close()` is called. To manage a closed state, I've added a `self._closed` 
attribute and flip that to `True` when `.close()` is called.



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