rdblue commented on a change in pull request #3691:
URL: https://github.com/apache/iceberg/pull/3691#discussion_r766257283



##########
File path: python/src/iceberg/io/s3.py
##########
@@ -0,0 +1,185 @@
+# 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.
+
+from typing import Union
+from urllib.parse import urlparse
+
+import boto3
+from botocore.errorfactory import ClientError
+
+from iceberg.io.base import FileIO, InputFile, OutputFile
+
+
+class S3InputFile(InputFile):
+    def __init__(self, location: str):
+        super().__init__(location=location)
+        self._s3_client = None
+
+    def __len__(self, s3_client=None):
+        s3 = self._create_s3_session() if not s3_client else s3_client
+        s3_url = urlparse(self.location)
+        resource = s3.head_object(Bucket=s3_url.netloc, 
Key=s3_url.path.lstrip("/"))
+        return resource["ContentLength"]
+
+    def _create_s3_session(self):
+        return boto3.Session().client("s3")
+
+    def exists(self, s3_client=None):
+        s3 = self._create_s3_session() if not s3_client else s3_client
+        s3_url = urlparse(self.location)
+        try:
+            s3.head_object(Bucket=s3_url.netloc, Key=s3_url.path.lstrip("/"))
+        except ClientError as e:
+            if "Not Found" in str(e):
+                return False
+            else:
+                raise
+
+        return True
+
+    def __call__(self, s3_client=None):
+        """Allows injecting an s3 client before the context is opened"""
+        self._s3_client = s3_client
+        return self
+
+    def __enter__(self):
+        """Enter context for S3InputFile
+
+        This sets a botocore.response.StreamingBody instance for the
+        location to self.input_stream. If an `s3_client` is not provided 
through
+        a call during context open, a boto3 session using the environment AWS
+        credentials will be created.
+
+        Example:
+            >>> input_file = S3InputFile(location="s3://foo/bar.json")
+                with input_file as f:
+                    data = input_file.input_stream.read()
+
+            >>> import boto3
+                AWS_ACCESS_KEY_ID = "..."
+                AWS_SECRET_ACCESS_KEY = "..."
+                s3_client = boto3.Session(
+                    aws_access_key_id=AWS_ACCESS_KEY_ID,
+                    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
+                ).client("s3")
+                input_file = S3InputFile(location="s3://baz/qux.json")
+                with input_file(s3_client=s3_client) as f:
+                    data = input_file.input_stream.read()
+
+
+        """
+        super().__enter__()
+        s3 = self._create_s3_session() if not self._s3_client else 
self._s3_client
+        s3_url = urlparse(self.location)
+        resource = s3.get_object(Bucket=s3_url.netloc, 
Key=s3_url.path.lstrip("/"))
+        return resource["Body"]
+
+    def __exit__(self, exc_type, exc_value, exc_traceback):
+        """A NOOP since boto3 automatically handles closing the session"""
+        super().__exit__(exc_type, exc_value, exc_traceback)
+        del self._s3_client
+        return
+
+
+class S3OutputFile(OutputFile):
+    def __init__(self, location: str):
+        super().__init__(location=location)
+        self._file_obj = None
+        self._s3_client = None
+        self._overwrite = False
+
+    def __call__(self, file_obj, overwrite: bool = False, **kwargs):
+        super().__call__(file_obj=file_obj, overwrite=overwrite)
+        self._s3_client = kwargs.get("s3_client")
+        return self
+
+    def __len__(self):
+        s3 = self._create_s3_session() if not self._s3_client else 
self._s3_client
+        s3_url = urlparse(self.location)
+        resource = s3.head_object(Bucket=s3_url.netloc, 
Key=s3_url.path.lstrip("/"))
+        return resource["ContentLength"]
+
+    def _create_s3_session(self):
+        return boto3.Session().client("s3")
+
+    def exists(self, s3_client=None):
+        s3 = self._create_s3_session() if not s3_client else s3_client
+        s3_url = urlparse(self.location)
+        try:
+            s3.head_object(Bucket=s3_url.netloc, Key=s3_url.path.lstrip("/"))
+        except ClientError as e:
+            if "Not Found" in str(e):
+                return False
+            else:
+                raise
+
+        return True
+
+    def to_input_file(self):
+        return S3InputFile(location=self._location)
+
+    def write(self):
+        s3 = self._create_s3_session() if not self._s3_client else 
self._s3_client
+        s3_url = urlparse(self.location)
+        return s3.upload_fileobj(
+            Fileobj=self._file_obj, Bucket=s3_url.netloc, 
Key=s3_url.path.lstrip("/")
+        )
+
+    def __enter__(self):
+        """Enter context for S3InputFile
+
+        This returns a botocore.response.StreamingBody instance for
+        the location. If an `s3_client` is not provided, a boto3 session
+        using the environment AWS credentials will be created.
+        """
+        super().__enter__()
+        if not self._file_obj:
+            raise RuntimeError(
+                "file_obj was not set. When using this context, use a call to 
set"
+                'a file object: `with 
S3OutputFile(file_obj="s3://foo/bar.json") as f:`'
+            )
+        return self
+
+    def __exit__(self, exc_type, exc_value, exc_traceback):
+        """A NOOP since boto3 automatically handles closing the session"""
+        super().__exit__(exc_type, exc_value, exc_traceback)
+        del self._file_obj
+        return
+
+
+class S3FileIO(FileIO):
+    def __init__(self, s3_client=None):
+        self._s3_client = boto3.Session().client("s3") if not s3_client else 
s3_client
+
+    def new_input(self, location: str):
+        super().new_input(location=location)
+        return S3InputFile(location=location)
+
+    def new_output(self, location: str):
+        super().new_output(location=location)
+        return S3OutputFile(location=location)
+
+    def delete(self, location: Union[str, S3InputFile, S3OutputFile]):
+        delete_location = (
+            location.location
+            if isinstance(location, (S3InputFile, S3OutputFile))
+            else location
+        )
+        super().delete(location=delete_location)
+        s3_url = urlparse(delete_location)
+        s3 = self._s3_client
+        return s3.delete_object(Bucket=s3_url.netloc, 
Key=s3_url.path.lstrip("/"))

Review comment:
       What does `delete_object` return?




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