Sftp Client
SFTP client for file transfer over SSH (draft-ietf-secsh-filexfer).
Obtain an instance via SshClient.openSftp. All methods are suspend functions for use with Kotlin coroutines. Multiple concurrent operations are supported via SFTP request pipelining.
All operations return SftpResult instead of throwing exceptions, so errors can be handled structurally. Use getOrNull or getOrThrow for convenience.
Usage:
val sftp = client.openSftp() ?: error("Failed to open SFTP")
try {
when (val result = sftp.listdir("/home/user")) {
is SftpResult.Success -> result.value.forEach { println(it.filename) }
is SftpResult.ServerError -> println("Error: ${result.message}")
is SftpResult.ProtocolError -> println("Protocol error: ${result.message}")
is SftpResult.IoError -> println("I/O error: ${result.cause}")
}
} finally {
sftp.close()
}Properties
SFTP protocol extensions the server advertised as extension-name/extension-data pairs trailing its SSH_FXP_VERSION reply (draft-ietf-secsh-filexfer-02 section 3). Only the names are kept; extension-specific data (if any) is discarded.
The negotiated SFTP protocol version (typically 3).
Functions
Close this SFTP session and the underlying SSH channel.
Close a file or directory handle.
Copies length bytes from srcHandle at srcOffset into dstHandle at dstOffset, entirely on the server — no data crosses the wire. This is the "copy-data" SFTP protocol extension OpenSSH added in 9.0 (April 2022); it lets the server use an efficient server-side copy (e.g. copy_file_range() on Linux) instead of the client reading the whole file and writing it back, and works even for accounts restricted to internal-sftp with no shell access (where server-side cp via SSH exec cannot run at all).
Set attributes of an open file handle.
Get attributes of an open file handle.
List all entries in a directory. Convenience method that handles opendir/readdir/close internally.
Get file attributes without following symlinks.
Create a directory.
Open a file. Returns a handle for subsequent read/write/close operations.
Open a directory for reading.
Read data from an open file at the given offset. Returns SftpResult.Success with data, or with null at EOF.
Read the next batch of directory entries. Returns SftpResult.Success with entries, or with null at end of directory.
Read the target of a symbolic link.
Resolve a path to its canonical absolute form.
Delete a file.
Remove an empty directory.
Set file attributes by path.
Get file attributes, following symlinks.
Write data to an open file at the given offset.