sftp
detdevlib.etl.sftp
SFTPConnection
An SFTP client for handling file transfers.
This class supports password and private key authentication.
__init__
__init__(
hostname: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
pkey_path: Optional[str | Path] = None,
pkey_pass: Optional[str] = None,
port: int = 22,
)
Initializes the SFTP connection.
Credentials can be provided or loaded from environment variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hostname
|
Optional[str]
|
The SFTP hostname. |
None
|
username
|
Optional[str]
|
The SFTP username. |
None
|
password
|
Optional[str]
|
The SFTP password. |
None
|
pkey_path
|
Optional[str | Path]
|
The path to the private key. |
None
|
pkey_pass
|
Optional[str]
|
The passphrase for the private key. |
None
|
port
|
int
|
The SFTP port. |
22
|
connect
connect() -> Self
Establishes an SFTP connection.
Returns:
| Type | Description |
|---|---|
Self
|
The instance of the SFTPConnection. |
close
close()
Closes the SFTP connection.
list_dir
list_dir(remote_path: str) -> list[str]
Lists the contents of a remote directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_path
|
str
|
The path to the remote directory. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of contents in the directory. |
exists
exists(remote_path: str) -> bool
Checks if a file or directory exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_path
|
str
|
The path to the remote destination. |
required |
upload
upload(local_path: str | Path, remote_path: str)
Uploads a file to the remote server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
str | Path
|
The path to the local file. |
required |
remote_path
|
str
|
The path to the remote destination. |
required |
download
download(remote_path: str, local_path: str | Path)
Downloads a remote file to the local machine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_path
|
str
|
The path to the remote destination. |
required |
local_path
|
str | Path
|
The path to the local file. |
required |
upload_bytes
upload_bytes(data: bytes, remote_path: str)
Uploads byte data from memory to a remote file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The byte content to upload. |
required |
remote_path
|
str
|
The path to the remote destination. |
required |
download_bytes
download_bytes(remote_path: str) -> bytes
Downloads a remote file's content into memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_path
|
str
|
The path to the remote file. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The content of the file as bytes. |
delete
delete(remote_path: str)
Deletes a file from the remote server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_path
|
str
|
The path to the remote destination. |
required |
rename
rename(old_remote_path: str, new_remote_path: str)
Renames or moves a file on the remote server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_remote_path
|
str
|
The path to the remote destination. |
required |
new_remote_path
|
str
|
The path to the new remote destination. |
required |