SftpClient

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

Link copied to clipboard
abstract val extensions: Set<String>

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.

Link copied to clipboard
abstract val isOpen: Boolean

Whether this SFTP session is still open.

Link copied to clipboard
abstract val protocolVersion: Int

The negotiated SFTP protocol version (typically 3).

Functions

Link copied to clipboard
abstract override fun close()

Close this SFTP session and the underlying SSH channel.

abstract suspend fun close(handle: SftpFileHandle): SftpResult<Unit>

Close a file or directory handle.

Link copied to clipboard
abstract suspend fun copyData(srcHandle: SftpFileHandle, srcOffset: Long, length: Long, dstHandle: SftpFileHandle, dstOffset: Long, timeoutMs: Long = 0): SftpResult<Unit>

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).

Link copied to clipboard
abstract suspend fun fsetstat(handle: SftpFileHandle, attrs: SftpAttributes): SftpResult<Unit>

Set attributes of an open file handle.

Link copied to clipboard
abstract suspend fun fstat(handle: SftpFileHandle): SftpResult<SftpAttributes>

Get attributes of an open file handle.

Link copied to clipboard
open suspend fun listdir(path: String): SftpResult<List<SftpDirectoryEntry>>

List all entries in a directory. Convenience method that handles opendir/readdir/close internally.

Link copied to clipboard
abstract suspend fun lstat(path: String): SftpResult<SftpAttributes>

Get file attributes without following symlinks.

Link copied to clipboard
abstract suspend fun mkdir(path: String, attrs: SftpAttributes = SftpAttributes.EMPTY): SftpResult<Unit>

Create a directory.

Link copied to clipboard
abstract suspend fun open(path: String, flags: Set<SftpOpenFlag>, attrs: SftpAttributes = SftpAttributes.EMPTY): SftpResult<SftpFileHandle>

Open a file. Returns a handle for subsequent read/write/close operations.

Link copied to clipboard
abstract suspend fun opendir(path: String): SftpResult<SftpFileHandle>

Open a directory for reading.

Link copied to clipboard
abstract suspend fun read(handle: SftpFileHandle, offset: Long, length: Int): SftpResult<ByteArray?>

Read data from an open file at the given offset. Returns SftpResult.Success with data, or with null at EOF.

Link copied to clipboard
abstract suspend fun readdir(handle: SftpFileHandle): SftpResult<List<SftpDirectoryEntry>?>

Read the next batch of directory entries. Returns SftpResult.Success with entries, or with null at end of directory.

Link copied to clipboard
abstract suspend fun readlink(path: String): SftpResult<String>

Read the target of a symbolic link.

Link copied to clipboard
abstract suspend fun realpath(path: String): SftpResult<String>

Resolve a path to its canonical absolute form.

Link copied to clipboard
abstract suspend fun remove(path: String): SftpResult<Unit>

Delete a file.

Link copied to clipboard
abstract suspend fun rename(oldPath: String, newPath: String): SftpResult<Unit>

Rename or move a file.

Link copied to clipboard
abstract suspend fun rmdir(path: String): SftpResult<Unit>

Remove an empty directory.

Link copied to clipboard
abstract suspend fun setstat(path: String, attrs: SftpAttributes): SftpResult<Unit>

Set file attributes by path.

Link copied to clipboard
abstract suspend fun stat(path: String): SftpResult<SftpAttributes>

Get file attributes, following symlinks.

Link copied to clipboard
abstract suspend fun symlink(targetPath: String, linkPath: String): SftpResult<Unit>

Create a symbolic link.

Link copied to clipboard
abstract suspend fun write(handle: SftpFileHandle, offset: Long, data: ByteArray): SftpResult<Unit>

Write data to an open file at the given offset.