airflow-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [airflow] mik-laj commented on a change in pull request #6393: [AIRFLOW-5718] Add SFTPToGoogleCloudStorageOperator
Date Fri, 01 Nov 2019 23:23:31 GMT
mik-laj commented on a change in pull request #6393: [AIRFLOW-5718] Add SFTPToGoogleCloudStorageOperator
URL: https://github.com/apache/airflow/pull/6393#discussion_r341781683
 
 

 ##########
 File path: airflow/operators/sftp_to_gcs.py
 ##########
 @@ -0,0 +1,181 @@
+#
+# 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.
+"""
+This module contains SFTP to Google Cloud Storage operator.
+"""
+import os
+from tempfile import NamedTemporaryFile
+from typing import Optional, Union
+
+from airflow import AirflowException
+from airflow.contrib.hooks.sftp_hook import SFTPHook
+from airflow.gcp.hooks.gcs import GoogleCloudStorageHook
+from airflow.models import BaseOperator
+from airflow.utils.decorators import apply_defaults
+
+WILDCARD = "*"
+
+
+class SFTPToGoogleCloudStorageOperator(BaseOperator):
+    """
+    Transfer files to Google Cloud Storage from SFTP server.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:SFTPToGoogleCloudStorageOperator`
+
+    :param source_path: The sftp remote path. This is the specified file path
+        for downloading the single file or multiple files from the SFTP server.
+        You can use only one wildcard within your path. The wildcard can appear
+        inside the path or at the end of the path.
+    :type source_path: str
+    :param destination_bucket: The bucket to upload to.
+    :type destination_bucket: str
+    :param destination_path: The destination name of the object in the
+        destination Google Cloud Storage bucket.
+        If destination_path is not provided file/files will be placed in the
+        main bucket path.
+        If a wildcard is supplied in the destination_path argument, this is the
+        prefix that will be prepended to the final destination objects' paths.
+    :type destination_path: str
+    :param gcp_conn_id: (Optional) The connection ID used to connect to Google Cloud Platform.
+    :type gcp_conn_id: str
+    :param sftp_conn_id: The sftp connection id. The name or identifier for
+        establishing a connection to the SFTP server.
+    :type sftp_conn_id: str
+    :param delegate_to: The account to impersonate, if any
+    :type delegate_to: str
+    :param mime_type: The mime-type string
+    :type mime_type: str
+    :param gzip: Allows for file to be compressed and uploaded as gzip
+    :type gzip: bool
+    :param move_object: When move object is True, the object is moved instead
+        of copied to the new location. This is the equivalent of a mv command
+        as opposed to a cp command.
+    :type move_object: bool
+    """
+
+    template_fields = ("source_path", "destination_path", "destination_bucket")
+
+    @apply_defaults
+    def __init__(
+        self,
+        source_path: str,
+        destination_bucket: str,
+        destination_path: Optional[str] = None,
+        gcp_conn_id: str = "google_cloud_default",
+        sftp_conn_id: str = "ssh_default",
+        delegate_to: Optional[str] = None,
+        mime_type: str = "application/octet-stream",
+        gzip: bool = False,
+        move_object: bool = False,
+        *args,
+        **kwargs
+    ) -> None:
+        super().__init__(*args, **kwargs)
+
+        self.source_path = source_path
+        self.destination_path = self._set_destination_path(destination_path)
+        self.destination_bucket = self._set_bucket_name(destination_bucket)
+        self.gcp_conn_id = gcp_conn_id
+        self.mime_type = mime_type
+        self.delegate_to = delegate_to
+        self.gzip = gzip
+        self.sftp_conn_id = sftp_conn_id
+        self.move_object = move_object
+
+    def execute(self, context):
+        gcs_hook = GoogleCloudStorageHook(
+            gcp_conn_id=self.gcp_conn_id, delegate_to=self.delegate_to
+        )
+
+        sftp_hook = SFTPHook(self.sftp_conn_id)
+
+        if WILDCARD in self.source_path:
+            total_wildcards = self.source_path.count(WILDCARD)
+            if total_wildcards > 1:
+                raise AirflowException(
+                    "Only one wildcard '*' is allowed in source_path parameter. "
+                    "Found {} in {}.".format(total_wildcards, self.source_path)
+                )
+
+            prefix, delimiter = self.source_path.split(WILDCARD, 1)
+            base_path = os.path.dirname(prefix)
+
+            files, _, _ = sftp_hook.get_tree_map(
+                base_path, prefix=prefix, delimiter=delimiter
+            )
+
+            for file in files:
+                destination_path = file.replace(base_path, self.destination_path, 1)
+                self._copy_single_object(gcs_hook, sftp_hook, file, destination_path)
+
+        else:
+            destination_object = os.path.join(self.destination_path, self.source_path)
+            self.log.info("destination_object %s", destination_object)
 
 Review comment:
   This line looks redundant.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message