SFTP Functions
This module contains functions for accessing remote files over SFTP (the SSH File Transfer Protocol) from XQuery.
It is based on the JSch library.
A session is opened with sftp:connect, which returns an opaque session handle used by subsequent functions, and closed with sftp:disconnect.
The module must be installed in the repository before it can be used. The version that matches BaseX is retrieved from the BaseX Maven Repository:
REPO INSTALL sftp
In order to use it, it needs to be imported in the header of the query:
import module namespace sftp = 'http://basex.org/modules/sftp';
All functions in this module are assigned to the http://basex.org/modules/sftp namespace, which in the following is assumed to be bound to the sftp prefix.
Updated: Errors are assigned to the module namespace.
Errors are assigned to the module namespace; their messages are prefixed with SFTP: .
Updated: Options Timeout, IdentityFile, Passphrase and KnownHosts added. Open sessions are closed after query evaluation, or when the query is stopped.
| Signature | sftp:connect( $server as xs:string, $user as xs:string, $password as xs:string, $port as xs:integer := 22, $options as map(*) := {}) as item() |
|---|
| Summary |
Opens an SFTP session against $server using the supplied credentials and returns an opaque session handle. With a private key supplied via IdentityFile, public-key authentication is tried before $password is used. If $port is omitted, the default port 22 is used. The $options map carries proxy, key and JSch session configuration. The following keys are recognized:
| Key |
Type |
Description |
ProxyHost |
xs:string |
HTTP proxy host. If supplied and non-empty, it overrides the BaseX PROXYHOST static option. |
ProxyPort |
xs:integer |
HTTP proxy port. If supplied, it overrides the BaseX PROXYPORT static option. |
Timeout |
xs:integer |
Timeout in milliseconds for establishing the connection. By default, no timeout is applied. |
IdentityFile |
xs:string |
Path to a private key file used for public-key authentication. |
Passphrase |
xs:string |
Passphrase of the private key; requires IdentityFile. If the key is encrypted and the passphrase is missing or incorrect, sftp:passphrase is raised. |
KnownHosts |
xs:string |
Path to a known_hosts file used to verify the host key of the server. |
| any other key |
xs:string |
Passed through to JSch as session configuration (for example StrictHostKeyChecking). |
When ProxyHost is absent from the map, the BaseX static options PROXYHOST and PROXYPORT are used as the fallback.
Sessions that are not closed with sftp:disconnect are closed after the query has been evaluated. If the query is stopped, its sessions are closed immediately, and pending operations are aborted.
|
|---|
| Errors | connect | The connection to the server could not be established, or authentication failed. | options | An option is invalid. | passphrase | The private key is encrypted, and no or an incorrect passphrase was supplied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
sftp:connect('sftp.example.org', 'user', 'password') Opens an SFTP session on the default port.
import module namespace sftp = 'http://basex.org/modules/sftp';
sftp:connect('sftp.example.org', 'user', '', 22, {
'IdentityFile': '/home/user/.ssh/id_ecdsa',
'KnownHosts': '/home/user/.ssh/known_hosts'
}) Opens an SFTP session with a private key and verifies the server against a known_hosts file.
import module namespace sftp = 'http://basex.org/modules/sftp';
sftp:connect('sftp.example.org', 'user', 'password', 2222) Opens an SFTP session on a custom port.
import module namespace sftp = 'http://basex.org/modules/sftp';
sftp:connect('sftp.example.org', 'user', 'password', 22,
{ 'StrictHostKeyChecking': 'no' }) Opens an SFTP session and disables strict host-key checking. |
|---|
| Signature | sftp:disconnect( $sftp as item()) as empty-sequence() |
|---|
| Summary | Closes the SFTP session associated with the supplied handle $sftp. |
|---|
| Errors | |
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return sftp:disconnect($sftp) Closes an SFTP session. |
|---|
| Signature | sftp:cd( $sftp as item(), $path as xs:string) as empty-sequence() |
|---|
| Summary | Changes the remote working directory of the session $sftp to $path. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:cd($sftp, '/home/user'),
sftp:disconnect($sftp)
) Changes into a remote directory. |
|---|
Added: New function.
| Signature | sftp:pwd( $sftp as item()) as xs:string |
|---|
| Summary | Returns the remote working directory of the session $sftp. |
|---|
| Errors | |
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $dir := (sftp:cd($sftp, 'reports'), sftp:pwd($sftp))
return (sftp:disconnect($sftp), $dir) Returns the absolute path of the reports directory. |
|---|
Updated: The entries . and .. are no longer returned.
| Signature | sftp:list( $sftp as item(), $path as xs:string) as xs:string* |
|---|
| Summary | Returns the file names contained in the remote directory $path, excluding . and ... |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $entries := sftp:list($sftp, '/home/user')
return (sftp:disconnect($sftp), $entries) Lists the contents of a remote directory. |
|---|
Added: New function.
| Signature | sftp:entries( $sftp as item(), $path as xs:string) as map(*)* |
|---|
| Summary | Returns a map for each entry of the remote directory $path, excluding . and ... Each map contains the entry name as name and the entries described in sftp:attributes; symbolic links are followed. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $files := sftp:entries($sftp, '/home/user')[not(?dir)]?name
return (sftp:disconnect($sftp), $files) Returns the names of all files in a remote directory, skipping subdirectories. |
|---|
Updated: Symbolic links are followed. New entries last-accessed, link, permissions, uid and gid.
| Signature | sftp:attributes( $sftp as item(), $path as xs:string) as map(*) |
|---|
| Summary |
Returns a map describing the remote file at $path. Symbolic links are followed; if the target does not exist, the attributes of the link are returned. The map contains the following entries:
| Key |
Type |
Description |
last-modified |
xs:dateTime |
Modification time. Only present if supplied by the server. |
size |
xs:integer |
File size in bytes. |
dir |
xs:boolean |
true if the entry is a directory. |
last-accessed |
xs:dateTime |
Access time. Only present if supplied by the server. |
link |
xs:boolean |
true if the entry is a symbolic link. |
permissions |
xs:string |
Permissions in symbolic notation (e.g. rwxr-xr-x). Only present if supplied by the server. |
uid, gid |
xs:integer |
Numeric user and group ID of the owner. Only present if supplied by the server. |
|
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $attr := sftp:attributes($sftp, '/home/user/readme.txt')
return (sftp:disconnect($sftp), $attr) Reads the attributes of a remote file. |
|---|
| Signature | sftp:move( $sftp as item(), $old-path as xs:string, $new-path as xs:string) as empty-sequence() |
|---|
| Summary | Moves a remote file from $old-path to $new-path. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:move($sftp, '/home/user/old.txt', '/home/user/new.txt'),
sftp:disconnect($sftp)
) Renames a remote file. |
|---|
| Signature | sftp:delete( $sftp as item(), $path as xs:string) as empty-sequence() |
|---|
| Summary | Removes the remote file at $path. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:delete($sftp, '/home/user/obsolete.txt'),
sftp:disconnect($sftp)
) Removes a remote file. |
|---|
| Signature | sftp:delete-dir( $sftp as item(), $path as xs:string) as empty-sequence() |
|---|
| Summary | Removes the remote directory at $path. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:delete-dir($sftp, '/home/user/old-folder'),
sftp:disconnect($sftp)
) Removes a remote directory. |
|---|
Added: New function.
| Signature | sftp:create-dir( $sftp as item(), $path as xs:string) as empty-sequence() |
|---|
| Summary | Creates the remote directory at $path, including all missing parent directories. Existing directories are left unchanged; an error is raised if a path segment exists as a file. |
|---|
| Errors | error | Processing failed. | no-dir | A segment of the specified path exists, but is no directory. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:create-dir($sftp, '/home/user/reports/2026'),
sftp:put-text($sftp, 'done', '/home/user/reports/2026/status.txt'),
sftp:disconnect($sftp)
) Creates a nested remote directory and uploads a file into it. |
|---|
Added: New function.
| Signature | sftp:exists( $sftp as item(), $path as xs:string) as xs:boolean |
|---|
| Summary | Checks if a remote file or directory exists at $path. Symbolic links are followed. |
|---|
| Errors | error | Processing failed. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $exists := sftp:exists($sftp, '/home/user/readme.txt')
return (sftp:disconnect($sftp), $exists) Checks if a remote file exists. |
|---|
Added: New function.
| Signature | sftp:chmod( $sftp as item(), $path as xs:string, $permissions as xs:string) as empty-sequence() |
|---|
| Summary | Changes the permissions of the remote file or directory at $path. $permissions is specified in octal notation (e.g. 755). |
|---|
| Errors | error | Processing failed. | format | The specified permissions are not in octal notation. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:chmod($sftp, '/home/user/script.sh', '750'),
sftp:disconnect($sftp)
) Makes a remote script executable for the owner and group. |
|---|
Updated: Parameter $encoding added; invalid XML characters are rejected.
| Signature | sftp:get-text( $sftp as item(), $path as xs:string, $encoding as xs:string := 'UTF-8') as xs:string |
|---|
| Summary | Retrieves the contents of the remote file at $path as a string, decoded with $encoding. An error is raised if the contents cannot be decoded or contain characters that are invalid in XML. |
|---|
| Errors | encoding | The specified encoding is not supported, a string cannot be represented in the encoding, or the contents cannot be decoded or contain invalid XML characters. | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $text := sftp:get-text($sftp, '/home/user/readme.txt')
return (sftp:disconnect($sftp), $text) Downloads a remote text file into a string. |
|---|
Added: New function.
| Signature | sftp:get-xml( $sftp as item(), $path as xs:string) as document-node() |
|---|
| Summary | Retrieves the remote file at $path and parses it as an XML document. The parsing options of the query are applied. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | parse | The remote file is not a well-formed XML document. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $doc := sftp:get-xml($sftp, '/home/user/feed.xml')
return (sftp:disconnect($sftp), $doc//title) Downloads a remote XML file and returns its titles. |
|---|
| Signature | sftp:get-binary( $sftp as item(), $path as xs:string) as xs:base64Binary |
|---|
| Summary | Retrieves the contents of the remote file at $path as a binary value. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $data := sftp:get-binary($sftp, '/home/user/image.png')
return (sftp:disconnect($sftp), $data) Downloads a remote binary file. |
|---|
| Signature | sftp:get-file( $sftp as item(), $path as xs:string, $local as xs:string) as empty-sequence() |
|---|
| Summary | Downloads the remote file at $path to the local filesystem at $local. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:get-file($sftp, '/home/user/data.zip', '/tmp/data.zip'),
sftp:disconnect($sftp)
) Downloads a remote file to local disk. |
|---|
Updated: Parameter $encoding added.
| Signature | sftp:put-text( $sftp as item(), $string as xs:string, $path as xs:string, $encoding as xs:string := 'UTF-8') as empty-sequence() |
|---|
| Summary | Uploads $string, encoded with $encoding, to the remote file at $path. |
|---|
| Errors | encoding | The specified encoding is not supported, a string cannot be represented in the encoding, or the contents cannot be decoded or contain invalid XML characters. | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:put-text($sftp, 'hello world', '/home/user/greeting.txt'),
sftp:disconnect($sftp)
) Uploads a string as a remote text file. |
|---|
Added: New function.
| Signature | sftp:append-text( $sftp as item(), $string as xs:string, $path as xs:string, $encoding as xs:string := 'UTF-8') as empty-sequence() |
|---|
| Summary | Appends $string, encoded with $encoding, to the remote file at $path. The file is created if it does not exist. |
|---|
| Errors | encoding | The specified encoding is not supported, a string cannot be represented in the encoding, or the contents cannot be decoded or contain invalid XML characters. | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:append-text($sftp, `{ current-dateTime() }
`, '/home/user/log.txt'),
sftp:disconnect($sftp)
) Appends a timestamp to a remote log file. |
|---|
| Signature | sftp:put-binary( $sftp as item(), $item as xs:base64Binary, $path as xs:string) as empty-sequence() |
|---|
| Summary | Uploads the binary value $item to the remote file at $path. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $bytes := xs:base64Binary('SGVsbG8=')
return (
sftp:put-binary($sftp, $bytes, '/home/user/hello.bin'),
sftp:disconnect($sftp)
) Uploads a binary value as a remote file. |
|---|
Added: New function.
| Signature | sftp:append-binary( $sftp as item(), $item as xs:base64Binary, $path as xs:string) as empty-sequence() |
|---|
| Summary | Appends the binary value $item to the remote file at $path. The file is created if it does not exist. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:append-binary($sftp, xs:base64Binary('SGVsbG8='), '/home/user/data.bin'),
sftp:disconnect($sftp)
) Appends bytes to a remote file. |
|---|
| Signature | sftp:put-file( $sftp as item(), $local as xs:string, $path as xs:string) as empty-sequence() |
|---|
| Summary | Uploads the local file at $local to the remote file at $path. |
|---|
| Errors | error | Processing failed. | not-found | The specified remote file or directory does not exist. | permission | Permission to access the remote file or directory was denied. |
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
return (
sftp:put-file($sftp, '/tmp/data.zip', '/home/user/data.zip'),
sftp:disconnect($sftp)
) Uploads a local file to the remote server. |
|---|
Updated: Only messages of the current query are returned.
| Signature | sftp:logs() as xs:string* |
|---|
| Summary |
Returns the JSch log messages of the sessions opened by the current query since the last call, then resets the internal log buffer. Each entry has the form <level>: <message>.
|
|---|
| Examples | import module namespace sftp = 'http://basex.org/modules/sftp';
let $sftp := sftp:connect('sftp.example.org', 'user', 'password')
let $entries := sftp:list($sftp, '/home/user')
return (
sftp:disconnect($sftp),
sftp:logs()
) Retrieves and clears the JSch log buffer after a session. |
|---|
| Code | Description |
|---|
connect | The connection to the server could not be established, or authentication failed. |
encoding | The specified encoding is not supported, a string cannot be represented in the encoding, or the contents cannot be decoded or contain invalid XML characters. |
error | Processing failed. |
format | The specified permissions are not in octal notation. |
no-dir | A segment of the specified path exists, but is no directory. |
not-found | The specified remote file or directory does not exist. |
options | An option is invalid. |
parse | The remote file is not a well-formed XML document. |
passphrase | The private key is encrypted, and no or an incorrect passphrase was supplied. |
permission | Permission to access the remote file or directory was denied. |
Version 14.0- Added:
sftp:pwd, sftp:entries, sftp:create-dir, sftp:exists, sftp:chmod, sftp:get-xml, sftp:append-text, sftp:append-binary. - Updated: Conventions: Errors are assigned to the module namespace.
- Updated:
sftp:connect: Options Timeout, IdentityFile, Passphrase and KnownHosts added for key-based authentication; open sessions are closed after query evaluation, or when the query is stopped. - Updated:
sftp:list: The entries . and .. are no longer returned. - Updated:
sftp:attributes: Symbolic links are followed. New entries last-accessed, link, permissions, uid and gid. - Updated:
sftp:get-text, sftp:put-text: Parameter $encoding added; sftp:get-text rejects invalid XML characters. - Updated:
sftp:logs: Only messages of the current query are returned.
⚡Generated with XQuery