Main Page » XQuery » Functions » SFTP Functions
BaseX 14 · This is the documentation of an upcoming version. Current version →

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.

Conventions

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

Session Management

sftp:connect

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
connectThe connection to the server could not be established, or authentication failed.
optionsAn option is invalid.
passphraseThe 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.

sftp:disconnect

Signature
sftp:disconnect(  $sftp  as item()) as empty-sequence()
SummaryCloses the SFTP session associated with the supplied handle $sftp.
Errors
errorProcessing failed.
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.

File and Directory Operations

sftp:cd

Signature
sftp:cd(  $sftp  as item(),  $path  as xs:string) as empty-sequence()
SummaryChanges the remote working directory of the session $sftp to $path.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:pwd

Added: New function.

Signature
sftp:pwd(  $sftp  as item()) as xs:string
SummaryReturns the remote working directory of the session $sftp.
Errors
errorProcessing failed.
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.

sftp:list

Updated: The entries . and .. are no longer returned.

Signature
sftp:list(  $sftp  as item(),  $path  as xs:string) as xs:string*
SummaryReturns the file names contained in the remote directory $path, excluding . and ...
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:entries

Added: New function.

Signature
sftp:entries(  $sftp  as item(),  $path  as xs:string) as map(*)*
SummaryReturns 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
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:attributes

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
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:move

Signature
sftp:move(  $sftp      as item(),  $old-path  as xs:string,  $new-path  as xs:string) as empty-sequence()
SummaryMoves a remote file from $old-path to $new-path.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:delete

Signature
sftp:delete(  $sftp  as item(),  $path  as xs:string) as empty-sequence()
SummaryRemoves the remote file at $path.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:delete-dir

Signature
sftp:delete-dir(  $sftp  as item(),  $path  as xs:string) as empty-sequence()
SummaryRemoves the remote directory at $path.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:create-dir

Added: New function.

Signature
sftp:create-dir(  $sftp  as item(),  $path  as xs:string) as empty-sequence()
SummaryCreates 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
errorProcessing failed.
no-dirA segment of the specified path exists, but is no directory.
permissionPermission 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.

sftp:exists

Added: New function.

Signature
sftp:exists(  $sftp  as item(),  $path  as xs:string) as xs:boolean
SummaryChecks if a remote file or directory exists at $path. Symbolic links are followed.
Errors
errorProcessing failed.
permissionPermission 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.

sftp:chmod

Added: New function.

Signature
sftp:chmod(  $sftp         as item(),  $path         as xs:string,  $permissions  as xs:string) as empty-sequence()
SummaryChanges the permissions of the remote file or directory at $path. $permissions is specified in octal notation (e.g. 755).
Errors
errorProcessing failed.
formatThe specified permissions are not in octal notation.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

File Transfer

sftp:get-text

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
SummaryRetrieves 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
encodingThe specified encoding is not supported, a string cannot be represented in the encoding, or the contents cannot be decoded or contain invalid XML characters.
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:get-xml

Added: New function.

Signature
sftp:get-xml(  $sftp  as item(),  $path  as xs:string) as document-node()
SummaryRetrieves the remote file at $path and parses it as an XML document. The parsing options of the query are applied.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
parseThe remote file is not a well-formed XML document.
permissionPermission 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.

sftp:get-binary

Signature
sftp:get-binary(  $sftp  as item(),  $path  as xs:string) as xs:base64Binary
SummaryRetrieves the contents of the remote file at $path as a binary value.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:get-file

Signature
sftp:get-file(  $sftp   as item(),  $path   as xs:string,  $local  as xs:string) as empty-sequence()
SummaryDownloads the remote file at $path to the local filesystem at $local.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:put-text

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()
SummaryUploads $string, encoded with $encoding, to the remote file at $path.
Errors
encodingThe specified encoding is not supported, a string cannot be represented in the encoding, or the contents cannot be decoded or contain invalid XML characters.
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:append-text

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()
SummaryAppends $string, encoded with $encoding, to the remote file at $path. The file is created if it does not exist.
Errors
encodingThe specified encoding is not supported, a string cannot be represented in the encoding, or the contents cannot be decoded or contain invalid XML characters.
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:put-binary

Signature
sftp:put-binary(  $sftp  as item(),  $item  as xs:base64Binary,  $path  as xs:string) as empty-sequence()
SummaryUploads the binary value $item to the remote file at $path.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:append-binary

Added: New function.

Signature
sftp:append-binary(  $sftp  as item(),  $item  as xs:base64Binary,  $path  as xs:string) as empty-sequence()
SummaryAppends the binary value $item to the remote file at $path. The file is created if it does not exist.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

sftp:put-file

Signature
sftp:put-file(  $sftp   as item(),  $local  as xs:string,  $path   as xs:string) as empty-sequence()
SummaryUploads the local file at $local to the remote file at $path.
Errors
errorProcessing failed.
not-foundThe specified remote file or directory does not exist.
permissionPermission 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.

Logging

sftp:logs

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.

Errors

CodeDescription
connectThe connection to the server could not be established, or authentication failed.
encodingThe specified encoding is not supported, a string cannot be represented in the encoding, or the contents cannot be decoded or contain invalid XML characters.
errorProcessing failed.
formatThe specified permissions are not in octal notation.
no-dirA segment of the specified path exists, but is no directory.
not-foundThe specified remote file or directory does not exist.
optionsAn option is invalid.
parseThe remote file is not a well-formed XML document.
passphraseThe private key is encrypted, and no or an incorrect passphrase was supplied.
permissionPermission to access the remote file or directory was denied.

Changelog

Version 14.0

⚡Generated with XQuery