Source code for airflow.providers.sftp.operators.sftp
## 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 operator."""importosfrompathlibimportPathfromtypingimportAnyfromairflow.exceptionsimportAirflowExceptionfromairflow.modelsimportBaseOperatorfromairflow.providers.ssh.hooks.sshimportSSHHook
[docs]classSFTPOperation:"""Operation that can be used with SFTP/"""
[docs]classSFTPOperator(BaseOperator):""" SFTPOperator for transferring files from remote host to local or vice a versa. This operator uses ssh_hook to open sftp transport channel that serve as basis for file transfer. :param ssh_hook: predefined ssh_hook to use for remote execution. Either `ssh_hook` or `ssh_conn_id` needs to be provided. :type ssh_hook: airflow.providers.ssh.hooks.ssh.SSHHook :param ssh_conn_id: :ref:`ssh connection id<howto/connection:ssh>` from airflow Connections. `ssh_conn_id` will be ignored if `ssh_hook` is provided. :type ssh_conn_id: str :param remote_host: remote host to connect (templated) Nullable. If provided, it will replace the `remote_host` which was defined in `ssh_hook` or predefined in the connection of `ssh_conn_id`. :type remote_host: str :param local_filepath: local file path to get or put. (templated) :type local_filepath: str :param remote_filepath: remote file path to get or put. (templated) :type remote_filepath: str :param operation: specify operation 'get' or 'put', defaults to put :type operation: str :param confirm: specify if the SFTP operation should be confirmed, defaults to True :type confirm: bool :param create_intermediate_dirs: create missing intermediate directories when copying from remote to local and vice-versa. Default is False. Example: The following task would copy ``file.txt`` to the remote host at ``/tmp/tmp1/tmp2/`` while creating ``tmp``,``tmp1`` and ``tmp2`` if they don't exist. If the parameter is not passed it would error as the directory does not exist. :: put_file = SFTPOperator( task_id="test_sftp", ssh_conn_id="ssh_default", local_filepath="/tmp/file.txt", remote_filepath="/tmp/tmp1/tmp2/file.txt", operation="put", create_intermediate_dirs=True, dag=dag ) :type create_intermediate_dirs: bool """
def__init__(self,*,ssh_hook=None,ssh_conn_id=None,remote_host=None,local_filepath=None,remote_filepath=None,operation=SFTPOperation.PUT,confirm=True,create_intermediate_dirs=False,**kwargs,)->None:super().__init__(**kwargs)self.ssh_hook=ssh_hookself.ssh_conn_id=ssh_conn_idself.remote_host=remote_hostself.local_filepath=local_filepathself.remote_filepath=remote_filepathself.operation=operationself.confirm=confirmself.create_intermediate_dirs=create_intermediate_dirsifnot(self.operation.lower()==SFTPOperation.GETorself.operation.lower()==SFTPOperation.PUT):raiseTypeError("unsupported operation value {}, expected {} or {}".format(self.operation,SFTPOperation.GET,SFTPOperation.PUT))
[docs]defexecute(self,context:Any)->str:file_msg=Nonetry:ifself.ssh_conn_id:ifself.ssh_hookandisinstance(self.ssh_hook,SSHHook):self.log.info("ssh_conn_id is ignored when ssh_hook is provided.")else:self.log.info("ssh_hook is not provided or invalid. Trying ssh_conn_id to create SSHHook.")self.ssh_hook=SSHHook(ssh_conn_id=self.ssh_conn_id)ifnotself.ssh_hook:raiseAirflowException("Cannot operate without ssh_hook or ssh_conn_id.")ifself.remote_hostisnotNone:self.log.info("remote_host is provided explicitly. ""It will replace the remote_host which was defined ""in ssh_hook or predefined in connection of ssh_conn_id.")self.ssh_hook.remote_host=self.remote_hostwithself.ssh_hook.get_conn()asssh_client:sftp_client=ssh_client.open_sftp()ifself.operation.lower()==SFTPOperation.GET:local_folder=os.path.dirname(self.local_filepath)ifself.create_intermediate_dirs:Path(local_folder).mkdir(parents=True,exist_ok=True)file_msg=f"from {self.remote_filepath} to {self.local_filepath}"self.log.info("Starting to transfer %s",file_msg)sftp_client.get(self.remote_filepath,self.local_filepath)else:remote_folder=os.path.dirname(self.remote_filepath)ifself.create_intermediate_dirs:_make_intermediate_dirs(sftp_client=sftp_client,remote_directory=remote_folder,)file_msg=f"from {self.local_filepath} to {self.remote_filepath}"self.log.info("Starting to transfer file %s",file_msg)sftp_client.put(self.local_filepath,self.remote_filepath,confirm=self.confirm)exceptExceptionase:raiseAirflowException(f"Error while transferring {file_msg}, error: {str(e)}")returnself.local_filepath
def_make_intermediate_dirs(sftp_client,remote_directory)->None:""" Create all the intermediate directories in a remote host :param sftp_client: A Paramiko SFTP client. :param remote_directory: Absolute Path of the directory containing the file :return: """ifremote_directory=='/':sftp_client.chdir('/')returnifremote_directory=='':returntry:sftp_client.chdir(remote_directory)exceptOSError:dirname,basename=os.path.split(remote_directory.rstrip('/'))_make_intermediate_dirs(sftp_client,dirname)sftp_client.mkdir(basename)sftp_client.chdir(basename)return