3
class SFTPOperation(object):
    PUT = 'put'
    GET = 'get'  

operation=SFTPOperation.GET,
NameError: name 'SFTPOperation' is not defined

I have defined operators here but I can't find anything on the internet related to operations

class sftpplugin(AirflowPlugin):
    name = "sftp_plugin"
    operators = [SFTPOperator]

Any help would appreciated!

Thanks,

Miguel Trejo
  • 5,913
  • 5
  • 24
  • 49
Raj
  • 585
  • 4
  • 16
  • 28

1 Answers1

8

By noticing that the SFTP operator uses ssh_hook to open an sftp transport channel, you should need to provide ssh_hook or ssh_conn_id for file transfer. First, let's see an example providing the parameter ssh_conn_id.

from airflow.providers.sftp.operators import sftp_operator
from airflow import DAG
import datetime

dag = DAG(
'test_dag',
start_date = datetime.datetime(2020,1,8,0,0,0),
schedule_interval = '@daily'
)

put_operation = SFTPOperator(
            task_id="operation",
            ssh_conn_id="ssh_default",
            local_filepath="route_to_local_file",
            remote_filepath="remote_route_to_copy",
            operation="put",
            dag=dag
            )
get_operation = SFTPOperator(....,
            operation = "get",
            dag = dag
            )

put_operation >> get_operation

Notice that the dag should be scheduled as needed by your task, here the example considers a daily schedule starting at noon. Now, If you're providing the SSHhook, the following changes to the above code are necessary

from airflow.contrib.hooks.ssh_hook import SSHHook
...

put_operation = SFTPOperator(
            task_id="operation",
            ssh_hook=SSHHook("Name_of_variable_defined"),
            ...
            dag=dag
            )
....

where "Name_of_variable_defined" is created in Admin -> Connections at the interface of Airflow.

Miguel Trejo
  • 5,913
  • 5
  • 24
  • 49
  • If I copy the entire script from this link into dag. It works well (sftp works well, moves file from remote to local) but if I save it as a plugin and import it, the script throws an error. https://airflow.apache.org/docs/stable/_modules/airflow/contrib/operators/sftp_operator.html – Raj Jan 08 '20 at 05:27
  • I have tried your given code and it throws an error 'module' object is not callable – Raj Jan 08 '20 at 05:54
  • The sftp_operator from contrib.operators is no longer in use. The new one is airflow.providers.sftp.operators.sftp_operator, i've just edited the code, it should be running correctly. For more info, check out https://github.com/apache/airflow/blob/master/airflow/providers/sftp/operators/sftp_operator.py – Miguel Trejo Jan 08 '20 at 06:10
  • 1
    can you provide some example for `ssh_conn_id` – Ravi H Feb 24 '21 at 16:13