k.net.sftpClient
SFTP file upload and download
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, background jobs | Reachable SFTP host with password or PEM private key | Syncing exports, importing backups, and transferring remote files |
k.net.sftpClient shares the same connection pattern as sshClient. Once connected, perform file operations via ConnectedClient inside the callback; the connection closes automatically when the callback completes.
Quick Start
k.api.post("syncReport", () => {
k.net.sftpClient.passwordConnect("sftp.example.com", "deploy", "password", (client) => {
const report = k.file.readBinary("exports/report.csv")
client.upload(report, "/incoming/report.csv")
}, 30)
return { uploaded: true }
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
passwordConnect(host, user, password, callback[, timeout]) | Connect to an SFTP host using password authentication | void |
privateKeyConnect(host, user, privateKey, callback[, timeout]) | Connect to an SFTP host using a PEM private key string | void |
client.upload(data, path) | Upload a byte array to a remote path inside the callback | void |
client.download(path) | Download a remote file as a byte array inside the callback | number[] |
passwordConnect() / privateKeyConnect()
passwordConnect(host, user, password, callback, timeout?) connects with a password; privateKeyConnect(host, user, privateKey, callback, timeout?) connects with a PEM private key string. Operate on files inside the callback; the connection closes after the callback finishes.
| Parameter | Type | Required | Description |
|---|---|---|---|
host | string | Yes | SFTP host name or IP |
user | string | Yes | Login user |
password | string | Required for passwordConnect | Login password |
privateKey | string | Required for privateKeyConnect | PEM private key content |
callback | (client) => void | Yes | Callback after the connection succeeds |
timeout | number | No | Connection timeout in seconds; default is 30 |
Returns: void. Use ConnectedClient inside callback to upload or download files.
k.net.sftpClient.passwordConnect(
"sftp.example.com",
"deploy",
"password",
(client) => {
const report = k.file.readBinary("exports/report.csv")
client.upload(report, "/incoming/report.csv")
const remoteCopy = client.download("/incoming/report.csv")
k.file.writeBinary("exports/report.remote-copy.csv", remoteCopy)
},
30
)When using key authentication, load the PEM private key from a non-public server-side store and pass it as a string:
Never store private keys in k.file
By default, Kooboo sites have EnableFileIOUrl = true, meaning files stored via k.file are publicly downloadable by anonymous HTTP clients at /__kb/kfile/<path> (unless EnableFileIOUrl is disabled or denyAnonymousFileIOAccessRule is configured). Store PEM private keys in a non-public store such as k.DB.keyValue (up to 4096 UTF-8 bytes per entry), k.DB.sqlite / k.DB.indexedDb, or server-only configuration, and never expose them via public endpoints.
const keyEntry = k.DB.keyValue.get("sftp_deploy_key")
const privateKey = keyEntry ? keyEntry.toString() : ""
if (!privateKey) {
throw new Error("Missing SFTP private key")
}
k.net.sftpClient.privateKeyConnect(
"sftp.example.com",
"deploy",
privateKey,
(client) => {
const bytes = client.download("/exports/latest.csv")
k.logger.information("SFTP", `Downloaded ${bytes.length} bytes`)
}
)ConnectedClient
Remote file session object passed into the connection callback.
| Method | Description |
|---|---|
upload(data, path) | Upload bytes to a remote path |
download(path) | Download a remote file as a byte array |
| Parameter | Type | Required | Description |
|---|---|---|---|
data | number[] | Required for upload | Bytes to upload |
path | string | Yes | Remote file path |
Returns: upload() returns void; download() returns number[].
let lineCount = 0
k.net.sftpClient.passwordConnect(host, user, password, (client) => {
const bytes = client.download("/exports/orders.csv")
const text = k.utils.bytesToString(bytes, "utf-8")
lineCount = text.split("\n").length
})
return { lineCount }TypeScript Definitions
interface SftpClient {
passwordConnect(host: string, user: string, password: string, callback: (p1: ConnectedClient) => void, timeout?: number): void;
privateKeyConnect(host: string, user: string, privateKey: string, callback: (p1: ConnectedClient) => void, timeout?: number): void;
}
interface ConnectedClient {
upload(data: number[], path: string): void;
download(path: string): number[];
}