Skip to content

SFTP File Transfer

connectx.sftp.SFTP

Handles downloading and uploading files from/to a remote server via SFTP or local file system paths.

Depending on the parameters, the class can either connect to a remote SFTP server using credentials or work with a local directory path.

__init__

Initializes an SFTP instance, optionally connecting to a remote server or preparing a local path for file operations.

Parameters:

Name Type Description Default
remote_path str

Remote directory or file to interact with.

'.'
host str

Hostname of the remote server (optional).

None
port int

Port of the remote server (optional).

None
username str

Username for SFTP authentication (optional).

None
password str

Encrypted password for SFTP authentication (optional).

None
local_path str | None

Local directory for downloaded/uploaded files. Defaults to "data/import".

'data/import'
remove_from_server bool

Whether to remove files from the remote server after download. Defaults to True.

True

backup

Backup downloaded files using move_and_delete_files_backup utility.

Returns:

Type Description

self

dir

Get a List of files from the remote path (supports SFTP or local paths).

Parameters:

Name Type Description Default
remote_path str | list[str]

The path to the file on the SFTP server that needs to be downloaded. Accept wildcard

None

Returns:

Type Description

A list of downloaded files. It downloads the specified file from the SFTP server to the local path.

download

Download files from the remote server or local path to a specified local directory.

Parameters:

Name Type Description Default
remote_path str | list

str or list of str, optional, path(s) of files to download. Defaults to the instance's remote_path.

None
local_path str

str, optional, directory to save downloaded files. Defaults to instance's local_path or temporary directory.

None

Returns:

Type Description

list of str, local file paths of the downloaded files.

ping

Simple health-check method to verify connectivity or readiness.

Calls the dir method (to list or initialize the directory context) and returns True to indicate successful execution.

Returns:

Type Description
bool

True if the method completes successfully.

purge

Remove downloaded files from the remote server or local path if remove_from_server is True.

Returns:

Type Description

self

upload

Upload files from a local directory to the remote server.

Parameters:

Name Type Description Default
local_path str | list

str or list of str, local file or directory to upload.

required
remote_path str

str, optional, target path on the remote server. Defaults to instance's remote_path.

None

Returns:

Type Description

list of str, paths of the files uploaded.