sftpUpload()
The function sftpUpload uploads a file to an SFTP (SSH File Transfer Protocol) server.
Synopsis
bool sftpUpload(string url, string remotePath, string localPath [mapping
options]);
Parameters
| Parameter | Description |
|---|---|
| url | The SFTP URL of the server (e.g.
ftp://myserver.com or
ftps://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 that should be uploaded. |
| options | Optional. A mapping with additional options for the SFTP transfer. See Options Mapping below. |
Return Value
The function returns TRUE if the upload was successful,
FALSE in case of an error.
Error
An error is given in case of missing or invalid arguments, a failed connection to the SFTP server, if the local file does not exist or the remote directory does not exist on the server.
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.
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 | - | FTP username. Overrides URL credentials if provided. |
| password | string | - | FTP 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)
|
| publicKeyPath | string | - | Path to public key file (necessary if private key file doesn't
contain public key information)
|
| 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 Upload
main()
{
bool result = sftpUpload("sftp://myserver.com", "/uploads/report.csv", "C:/data/report.csv");
if (result)
DebugN("Upload successful");
else
DebugN("Upload failed");
}
SFTP Upload with Options
main()
{
mapping opts;
opts["username"] = "ftpuser";
opts["password"] = "secret";
opts["timeout"] = 30;
bool result = sftpUpload("sftp://secureserver.com", "/uploads/daily.csv", "C:/data/daily.csv", opts);
if (result)
DebugN("Secure upload successful");
else
DebugN("Secure upload failed");
}
Assignment
File functionsAvailability
UI, CTRL
