You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/05/30 15:30:39 UTC

[GitHub] [iceberg] samredai commented on a diff in pull request #4792: Python: Add BotoFileIO, a FileIO that wraps boto3

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


##########
python/src/iceberg/io/boto.py:
##########
@@ -0,0 +1,294 @@
+# 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 typing import Literal, Optional, Union
+from urllib.parse import urlparse
+
+from boto3 import Session
+from boto3.resources.factory import s3
+from botocore.exceptions import ClientError
+
+from iceberg.io.base import FileIO, InputFile, InputStream, OutputFile, OutputStream
+
+class BotoInputStream:
+    """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: s3.Object):
+        self._s3_object = s3_object
+        self._position = 0
+
+    def read(self, size: 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:
+            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
+        """
+        if size == -1:  # 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 + size
+
+            if position_new >= self.size:  # If more bytes are requested than exists, just read the entire file from the current position
+                return self.read(size=-1)
+
+            range_header = f"bytes={self._position}-{position_new -1}"
+            self.seek(offset=size, whence=1)
+
+        return self._s3_object.get(Range=range_header)["Body"].read()
+
+    def seek(self, offset: int, whence: Literal[0, 1, 2] = 0) -> None:
+        position_new = offset if whence == 0 else self._position + offset if whence == 1 else self._s3_object.content_length + offset if whence == 2 else None
+
+        if not position_new:
+            raise ValueError(f"Cannot seek to position {offset}, invalid whence: {whence}")
+
+        self._position = position_new
+        return self._position
+
+    def tell(self) -> int:
+        return self._position
+
+    def closed(self) -> bool:
+        return False
+
+    def close(self) -> None:
+        pass
+
+class BotoOutputStream:
+    """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)
+
+    def closed(self) -> bool:
+        """Returns where the stream is closed or not
+
+        Since this is a wrapper for requests to S3, there is no concept of closing, therefore this always returns False
+        """
+        return False
+
+    def close(self) -> None:
+        """Closes the stream
+        
+        Since this is a wrapper for requests to S3, there is no concept of closing, therefore this method does nothing
+        """
+        pass
+
+class BotoInputFile(InputFile):
+    """An input file implementation for the BotoFileIO
+
+    Args:
+        location(str): An S3 URI
+
+    Attributes:
+        location(str): An S3 URI
+
+    Examples:
+        >>> from iceberg.io.boto import BotoInputFile
+        >>> input_file = BotoInputFile("s3://foo/bar.txt")
+        >>> file_content = input_file.open().read()  # Read the contents of the BotoInputFile instance
+    """
+
+    def __init__(self, location: str, session: Session):
+        parsed_location = urlparse(location)  # Create a ParseResult from the uri
+
+        if not parsed_location.scheme.startswith('s3'):
+            raise ValueError(f"Cannot create BotoInputFile, scheme not supported: {parsed_location.scheme}")
+
+        self._bucket = parsed_location.netloc
+        self._path = parsed_location.path.strip("/")
+        self._session = session
+        super().__init__(location=location)
+
+    def __len__(self) -> int:
+        """Returns the total length of the file, in bytes"""
+        file_info = self._file_info()
+        return file_info.size
+
+    def exists(self) -> bool:
+        """Checks whether the location exists"""
+        try:
+            self._session.resource('s3').Bucket(self._bucket).Object(self._path).load()  # raises botocore.exceptions.ClientError with a 404 if it does not exist

Review Comment:
   Ah you're absolutely right. But since we use the session object directly to create resource clients we'll probably need to add an endpoint argument to the `BotoFileIO` init and then consistently pass it to `resource(...)` which also has an [endpoint_url](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html#boto3.session.Session.resource) argument. Let me update this, thanks @joshuarobinson!



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org