sftpDownload()

The function sftpDownload downloads a file from an SFTP (SSH File Transfer Protocol) server.

Synopsis

bool sftpDownload(string url, string remotePath, string localPath [mapping options]);

Parameters

Parameter Description
url The SFTP URL of the server (e.g. sftp://myserver.com.
remotePath Path to the file on the SFTP server. The folder(s) must exist on the server.
localPath Path to the local file. The file will be overwritten if it already exists or created if it does not exist yet.
options Optional. A mapping with additional options for the SFTP transfer. See Options Mapping below.

Return Value

The function returns TRUE if the download was successful, FALSE in case of an error.

Error

An error is given in case of missing or invalid arguments, failed connection to the SFTP server or when the remote file does not exist.

Details

Credentials can either be specified as part of the URL (e.g. sftp://user:password@myserver.com) or via the username and password keys in the options mapping. If both are provided, the options mapping values take precedence.

Note:
SFTP (SSH File Transfer Protocol) functions are not to be confused with FTPS (File Transfer Protocol with SSL/TLS-encryption). For FTP/FTPS functions, see ftpUpload()/ftpDownload().

Options Mapping

The optional options parameter is a mapping that can contain the following keys:

Key Type Default Description
timeout int 0 Total operation timeout in seconds. A value of 0 means unlimited.
username string - SFTP username. Overrides URL credentials if provided.
password string - SFTP password. Overrides URL credentials if provided.
authMode int 2 (Auto) Authentication mode (0=Password, 1=Public Key, 2=Auto)
privateKeyPath string - Path to private key file (for public key auth)
  • Supported Formats: OpenSSH, PEM
publicKeyPath string - Path to public key file (necessary if private key file doesn't contain public key information)
  • Supported Formats: OpenSSH, PEM
knownHostsPath string - Path to known_hosts file for host verification (has to be in ASCII or UTF-8)
verifyHost bool TRUE Verify server host key against known_hosts

Simple SFTP Download

main()
{
  bool result = sftpDownload("sftp://myserver.com", "/data/report.csv", "C:/temp/report.csv");
  if (result)
    DebugN("Download successful");
  else
    DebugN("Download failed");
}

SFTP Download with Options

main()
{
  mapping opts;
  opts["username"] = "ftpuser";
  opts["password"] = "secret";
  opts["timeout"] = 30;

  bool result = sftpDownload("sftp://secureserver.com", "/reports/daily.csv", "C:/temp/daily.csv", opts);
  if (result)
    DebugN("Secure download successful");
  else
    DebugN("Secure download failed");
}

Assignment

File functions

Availability

UI, CTRL