class_docstr: SFTPOperator for transferring files from remote host to local or vice a versa.
  
  This operator uses sftp_hook to open sftp transport channel that serve as basis for file transfer.
  
  :param ssh_conn_id: :ref:`ssh connection id<howto/connection:ssh>`
      from airflow Connections. `ssh_conn_id` will be ignored if `ssh_hook`
      or `sftp_hook` is provided.
  :param sftp_hook: predefined SFTPHook to use
      Either `sftp_hook` or `ssh_conn_id` needs to be provided.
  :param ssh_hook: Deprecated - predefined SSHHook to use for remote execution
      Use `sftp_hook` instead.
  :param remote_host: remote host to connect (templated)
      Nullable. If provided, it will replace the `remote_host` which was
      defined in `sftp_hook`/`ssh_hook` or predefined in the connection of `ssh_conn_id`.
  :param local_filepath: local file path or list of local file paths to get or put. (templated)
  :param remote_filepath: remote file path or list of remote file paths to get or put. (templated)
  :param operation: specify operation 'get' or 'put', defaults to put
  :param confirm: specify if the SFTP operation should be confirmed, defaults to True
  :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,
          )
class_docstr_tok: ["SFTPOperator","for","transferring","files","from","remote","host","to","local","or","vice","a","versa",".","This","operator","uses","sftp_hook","to","open","sftp","transport","channel","that","serve","as","basis","for","file","transfer",".",":","param","ssh_conn_id",":",":","ref",":","`","ssh","connection","id","<","howto\/connection",":","ssh",">","`","from","airflow","Connections",".","`","ssh_conn_id","`","will","be","ignored","if","`","ssh_hook","`","or","`","sftp_hook","`","is","provided",".",":","param","sftp_hook",":","predefined","SFTPHook","to","use","Either","`","sftp_hook","`","or","`","ssh_conn_id","`","needs","to","be","provided",".",":","param","ssh_hook",":","Deprecated","-","predefined","SSHHook","to","use","for","remote","execution","Use","`","sftp_hook","`","instead",".",":","param","remote_host",":","remote","host","to","connect","(","templated",")","Nullable",".","If","provided",",","it","will","replace","the","`","remote_host","`","which","was","defined","in","`","sftp_hook","`","\/","`","ssh_hook","`","or","predefined","in","the","connection","of","`","ssh_conn_id","`",".",":","param","local_filepath",":","local","file","path","or","list","of","local","file","paths","to","get","or","put",".","(","templated",")",":","param","remote_filepath",":","remote","file","path","or","list","of","remote","file","paths","to","get","or","put",".","(","templated",")",":","param","operation",":","specify","operation","'get","'","or","'put","'",",","defaults","to","put",":","param","confirm",":","specify","if","the","SFTP","operation","should","be","confirmed",",","defaults","to","True",":","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","do","n'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",",",")"]
code: def execute(self, context: Any) -> str | list[str] | None:
      if isinstance(self.local_filepath, str):
          local_filepath_array = [self.local_filepath]
      else:
          local_filepath_array = self.local_filepath
  
      if isinstance(self.remote_filepath, str):
          remote_filepath_array = [self.remote_filepath]
      else:
          remote_filepath_array = self.remote_filepath
  
      if len(local_filepath_array) != len(remote_filepath_array):
          raise ValueError(
              f"{len(local_filepath_array)} paths in local_filepath "
              f"!= {len(remote_filepath_array)} paths in remote_filepath"
          )
  
      if self.operation.lower() not in (
          SFTPOperation.GET,
          SFTPOperation.PUT,
      ):
          raise TypeError(
              f"Unsupported operation value {self.operation}, "
              f"expected {SFTPOperation.GET} or {SFTPOperation.PUT}."
          )
  
      # TODO: remove support for ssh_hook in next major provider version in hook and operator
      if self.ssh_hook is not None and self.sftp_hook is not None:
          raise AirflowException(
              "Both `ssh_hook` and `sftp_hook` are defined. Please use only one of them."
          )
  
      if self.ssh_hook is not None:
          if not isinstance(self.ssh_hook, SSHHook):
              self.log.info(
                  "ssh_hook is invalid. Trying ssh_conn_id to create SFTPHook."
              )
              self.sftp_hook = SFTPHook(ssh_conn_id=self.ssh_conn_id)
          if self.sftp_hook is None:
              warnings.warn(
                  "Parameter `ssh_hook` is deprecated. "
                  "Please use `sftp_hook` instead. "
                  "The old parameter `ssh_hook` will be removed in a future version.",
                  AirflowProviderDeprecationWarning,
                  stacklevel=2,
              )
              self.sftp_hook = SFTPHook(ssh_hook=self.ssh_hook)
  
      file_msg = None
      try:
          if self.ssh_conn_id:
              if self.sftp_hook and isinstance(
                  self.sftp_hook, SFTPHook
              ):
                  self.log.info(
                      "ssh_conn_id is ignored when sftp_hook/ssh_hook is provided."
                  )
              else:
                  self.log.info(
                      "sftp_hook/ssh_hook not provided or invalid. Trying ssh_conn_id to create SFTPHook."
                  )
                  self.sftp_hook = SFTPHook(
                      ssh_conn_id=self.ssh_conn_id
                  )
  
          if not self.sftp_hook:
              raise AirflowException(
                  "Cannot operate without sftp_hook or ssh_conn_id."
              )
  
          if self.remote_host is not None:
              self.log.info(
                  "remote_host is provided explicitly. "
                  "It will replace the remote_host which was defined "
                  "in sftp_hook or predefined in connection of ssh_conn_id."
              )
              self.sftp_hook.remote_host = self.remote_host
  
          for _local_filepath, _remote_filepath in zip(
              local_filepath_array, remote_filepath_array
          ):
              if self.operation.lower() == SFTPOperation.GET:
                  local_folder = os.path.dirname(_local_filepath)
                  if self.create_intermediate_dirs:
                      Path(local_folder).mkdir(
                          parents=True, exist_ok=True
                      )
                  file_msg = (
                      f"from {_remote_filepath} to {_local_filepath}"
                  )
                  self.log.info("Starting to transfer %s", file_msg)
                  self.sftp_hook.retrieve_file(
                      _remote_filepath, _local_filepath
                  )
              else:
                  remote_folder = os.path.dirname(_remote_filepath)
                  if self.create_intermediate_dirs:
                      self.sftp_hook.create_directory(remote_folder)
                  file_msg = (
                      f"from {_local_filepath} to {_remote_filepath}"
                  )
                  self.log.info(
                      "Starting to transfer file %s", file_msg
                  )
                  self.sftp_hook.store_file(
                      _remote_filepath,
                      _local_filepath,
                      confirm=self.confirm,
                  )
  
      except Exception as e:
          raise AirflowException(
              f"Error while transferring {file_msg}, error: {e}"
          )
  
      return self.local_filepath
  
file_path: airflow/airflow/providers/sftp/operators/sftp.py
imports: from __future__ import annotations
  import os
  import socket
  import warnings
  from pathlib import Path
  from typing import Any, Sequence
  import paramiko
  from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning
  from airflow.models import BaseOperator
  from airflow.providers.sftp.hooks.sftp import SFTPHook
  from airflow.providers.ssh.hooks.ssh import SSHHook
label_desc: Use image node_id 2 for calling the SFTPOperator obj's underlying member method code with example usage: obj.execute(context) and returns: self