Skip to main content

Actions

The ballerina/ftp package exposes the following clients:

ClientPurpose
ClientConnects to FTP/FTPS/SFTP servers for file read/write, directory management, and file operations.

For event-driven integration, see the Trigger Reference.


Client

Connects to FTP/FTPS/SFTP servers for file read/write, directory management, and file operations.

Configuration

FieldTypeDefaultDescription
protocolProtocolFTPThe file transfer protocol to use (FTP, FTPS, or SFTP).
hoststring"127.0.0.1"Hostname or IP address of the FTP server.
portint21Port number of the FTP server.
authAuthConfiguration()Authentication configuration including credentials, private key, and secure socket settings.
userDirIsRootbooleanfalseWhether to treat the user's home directory as the root directory.
laxDataBindingbooleanfalseIf true, enables relaxed data binding: null values in JSON/XML map to optional fields, and missing fields map to null values.
connectTimeoutdecimal30.0Connection timeout in seconds.
socketConfigSocketConfig()Socket timeout configuration for FTP data, FTP socket, and SFTP session timeouts.
proxyProxyConfiguration()Proxy server configuration for the connection (SFTP only).
fileTransferModeFileTransferModeBINARYFile transfer mode (BINARY or ASCII) (FTP only).
sftpCompressionTransferCompression[][NO]Compression algorithms for SFTP transfers (ZLIB, ZLIBOPENSSH, or NO).
sftpSshKnownHostsstring()Path to the SSH known hosts file for SFTP host key verification.
csvFailSafeFailSafeOptions()Fail-safe CSV content processing. When set, malformed CSV records are skipped and written to a separate file in the current directory instead of failing the operation.
retryConfigRetryConfig()Retry configuration for transient failures on non-streaming read operations (getBytes, getText, getJson, getXml, getCsv). If not specified, no retry is attempted.
circuitBreakerCircuitBreakerConfig()Circuit breaker for client operations. When the failure ratio exceeds the configured threshold, the client fails fast without contacting the server until the breaker resets.

Initializing the client

import ballerina/ftp;

configurable string host = ?;
configurable int port = ?;
configurable string username = ?;
configurable string password = ?;

ftp:Client ftpClient = check new ({
protocol: ftp:FTP,
host: host,
port: port,
auth: {
credentials: {
username: username,
password: password
}
}
});

Operations

File reading

getBytes

Retrieves the content of a remote file as a byte array.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the remote file.

Returns: byte[]|Error

Sample code:

byte[] content = check ftpClient->getBytes("/home/user/files/data.bin");

Sample response:

[72, 101, 108, 108, 111]
getText

Retrieves the content of a remote file as a string.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the remote file.

Returns: string|Error

Sample code:

string content = check ftpClient->getText("/home/user/files/readme.txt");

Sample response:

"Hello, this is the content of the file."
getJson

Retrieves the content of a remote file as a JSON value with optional data binding to a record type.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the remote file.
targetTypetypedesc<json|record {}>NoThe target type for data binding.

Returns: targetType|Error

Sample code:

json data = check ftpClient->getJson("/home/user/files/config.json");

Sample response:

{"name": "App Config", "version": "1.0", "debug": true}
getXml

Retrieves the content of a remote file as an XML value with optional data binding to a record type.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the remote file.
targetTypetypedesc<xml|record {}>NoThe target type for data binding.

Returns: targetType|Error

Sample code:

xml data = check ftpClient->getXml("/home/user/files/data.xml");

Sample response:

<config><name>App Config</name><version>1.0</version></config>
getCsv

Retrieves the content of a remote CSV file with optional data binding to a record array type.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the remote CSV file.
targetTypetypedesc<string[][]|record {}[]>NoThe target type for data binding.

Returns: targetType|Error

Sample code:

string[][] data = check ftpClient->getCsv("/home/user/files/report.csv");

Sample response:

[["Name", "Age", "City"], ["Alice", "30", "New York"], ["Bob", "25", "London"]]
getBytesAsStream

Retrieves the content of a remote file as a byte stream, suitable for large files.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the remote file.

Returns: stream<byte[], error?>|Error

Sample code:

stream<byte[], error?> byteStream = check ftpClient->getBytesAsStream("/home/user/files/large-file.zip");
getCsvAsStream

Retrieves the content of a remote CSV file as a stream of rows, suitable for large files.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the remote CSV file.
targetTypetypedesc<string[]|record {}>NoThe target type for each row.

Returns: stream<targetType, error?>|Error

Sample code:

stream<string[], error?> csvStream = check ftpClient->getCsvAsStream("/home/user/files/large-report.csv");

File writing

putBytes

Writes byte content to a remote file. Supports overwrite and append modes.

Parameters:

NameTypeRequiredDescription
pathstringYesThe destination file path on the remote server.
contentbyte[]YesThe byte content to write.
optionFileWriteOptionNoWrite mode with OVERWRITE (default) or APPEND.

Returns: Error?

Sample code:

check ftpClient->putBytes("/home/user/files/output.bin", [72, 101, 108, 108, 111]);
putText

Writes string content to a remote file. Supports overwrite and append modes.

Parameters:

NameTypeRequiredDescription
pathstringYesThe destination file path on the remote server.
contentstringYesThe text content to write.
optionFileWriteOptionNoWrite mode with OVERWRITE (default) or APPEND.

Returns: Error?

Sample code:

check ftpClient->putText("/home/user/files/readme.txt", "Hello, World!");
putJson

Writes JSON content to a remote file. Supports overwrite and append modes.

Note that APPEND performs raw text concatenation, which can produce invalid JSON/XML. Use OVERWRITE unless you specifically intend to concatenate text fragments.

Parameters:

NameTypeRequiredDescription
pathstringYesThe destination file path on the remote server.
contentjson|record {}YesThe JSON content to write.
optionFileWriteOptionNoWrite mode with OVERWRITE (default) or APPEND.

Returns: Error?

Sample code:

check ftpClient->putJson("/home/user/files/config.json", {"name": "App", "version": "2.0"});
putXml

Writes XML content to a remote file. Supports overwrite and append modes.

Note that APPEND performs raw text concatenation, which can produce invalid JSON/XML. Use OVERWRITE unless you specifically intend to concatenate text fragments.

Parameters:

NameTypeRequiredDescription
pathstringYesThe destination file path on the remote server.
contentxml|record {}YesThe XML content to write.
optionFileWriteOptionNoWrite mode with OVERWRITE (default) or APPEND.

Returns: Error?

Sample code:

check ftpClient->putXml("/home/user/files/data.xml", xml `<config><name>App</name></config>`);
putCsv

Writes CSV content to a remote file. Supports overwrite and append modes.

Parameters:

NameTypeRequiredDescription
pathstringYesThe destination file path on the remote server.
contentstring[][]|record {}[]YesThe CSV content as a 2D string array or record array.
optionFileWriteOptionNoWrite mode with OVERWRITE (default) or APPEND.

Returns: Error?

Sample code:

check ftpClient->putCsv("/home/user/files/report.csv", [
["Name", "Age", "City"],
["Alice", "30", "New York"],
["Bob", "25", "London"]
]);
putBytesAsStream

Writes a byte stream to a remote file, suitable for large files.

Parameters:

NameTypeRequiredDescription
pathstringYesThe destination file path on the remote server.
contentstream<byte[], error?>YesThe byte stream to write.
optionFileWriteOptionNoWrite mode with OVERWRITE (default) or APPEND.

Returns: Error?

Sample code:

stream<byte[], error?> byteStream = check ftpClient->getBytesAsStream("/home/user/files/source.dat");
check ftpClient->putBytesAsStream("/home/user/files/destination.dat", byteStream);
putCsvAsStream

Writes a CSV stream to a remote file, suitable for large datasets.

Parameters:

NameTypeRequiredDescription
pathstringYesThe destination file path on the remote server.
contentstream<string[]|record {}, error?>YesThe CSV row stream to write.
optionFileWriteOptionNoWrite mode with OVERWRITE (default) or APPEND.

Returns: Error?

Sample code:

stream<string[], error?> csvStream = check ftpClient->getCsvAsStream("/home/user/files/source.csv");
check ftpClient->putCsvAsStream("/home/user/files/destination.csv", csvStream);

Directory operations

list

Lists the files and directories in the specified remote directory.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the remote directory.

Returns: FileInfo[]|Error

Sample code:

ftp:FileInfo[] files = check ftpClient->list("/home/user/files");

Sample response:

[{"path": "/home/user/files/readme.txt", "size": 1024, "lastModifiedTimestamp": 1711324800000, "name": "readme.txt", "isFolder": false, "isFile": true}, {"path": "/home/user/files/data", "size": 0, "lastModifiedTimestamp": 1711238400000, "name": "data", "isFolder": true, "isFile": false, ...}]
mkdir

Creates a new directory on the remote server.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the directory to create.

Returns: Error?

Sample code:

check ftpClient->mkdir("/home/user/files/new-directory");
rmdir

Removes an empty directory from the remote server.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the directory to remove.

Returns: Error?

Sample code:

check ftpClient->rmdir("/home/user/files/old-directory");
isDirectory

Checks whether the specified remote path is a directory.

Parameters:

NameTypeRequiredDescription
pathstringYesThe remote path to check.

Returns: boolean|Error

Sample code:

boolean isDir = check ftpClient->isDirectory("/home/user/files/data");

Sample response:

true

File management

rename

Renames a file or directory on the remote server.

Parameters:

NameTypeRequiredDescription
originstringYesThe current path of the file or directory.
destinationstringYesThe new path for the file or directory.

Returns: Error?

Sample code:

check ftpClient->rename("/home/user/files/old-name.txt", "/home/user/files/new-name.txt");
move

Moves a file from one location to another on the remote server.

Parameters:

NameTypeRequiredDescription
sourcePathstringYesThe current file path.
destinationPathstringYesThe target file path.

Returns: Error?

Sample code:

check ftpClient->move("/home/user/inbox/report.csv", "/home/user/processed/report.csv");
copy

Copies a file from one location to another on the remote server.

Parameters:

NameTypeRequiredDescription
sourcePathstringYesThe source file path.
destinationPathstringYesThe destination file path.

Returns: Error?

Sample code:

check ftpClient->copy("/home/user/files/report.csv", "/home/user/backup/report.csv");
delete

Deletes a file from the remote server.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the file to delete.

Returns: Error?

Sample code:

check ftpClient->delete("/home/user/files/old-report.csv");
exists

Checks whether a file or directory exists at the specified remote path.

Parameters:

NameTypeRequiredDescription
pathstringYesThe remote path to check.

Returns: boolean|Error

Sample code:

boolean fileExists = check ftpClient->exists("/home/user/files/report.csv");

Sample response:

true
size

Retrieves the size of a remote file in bytes.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path of the remote file.

Returns: int|Error

Sample code:

int fileSize = check ftpClient->size("/home/user/files/report.csv");

Sample response:

204800

Connection management

close

Closes the connection to the remote FTP/FTPS/SFTP server and releases resources.

Returns: Error?

Sample code:

check ftpClient->close();