import ballerina/ftp;
import ballerina/io;
import ballerina/lang.'string as strings;

public function main() returns error? {
    // Creates the client with the connection parameters, host, username, and
    // password. An error is returned in a failure. The default port number
    // `22` for SSH is used with these configurations.
    ftp:ClientConfiguration config = {
        protocol: ftp:SFTP,
        host: "sftp.example.com",
        port: 22,
        auth: {
            credentials: {username: "user1", password: "pass456"},

            // Private key file location and its password (if encrypted) is
            // given corresponding to the SSH key file used in the SFTP client.
            privateKey: {
                path: "../resource/path/to/private.key",
                password: "keyPass123"
            }
        }
    };
    ftp:Client clientEp = check new(config);

    // Reads a file from a FTP server for a given file path. In error cases, 
    // an error is returned.
    stream<byte[] & readonly, io:Error?> fileStream
        = check clientEp->get("/server/book.txt");
    check fileStream.forEach(isolated 
        function(byte[] & readonly fileContent) {
            io:println("File content received: "
                + checkpanic strings:fromBytes(fileContent));
        }
    );

    // Add a new file to the given file location. In error cases, 
    // an error is returned. The local file is provided as a stream of
    // `io:Block` in which 1024 is the block size.
    stream<io:Block, io:Error?> bStream
        = check io:fileReadBlocksAsStream("/local/logFile.txt", 1024);
    check clientEp->put("/server", bStream);

    // Closes the file stream to finish the `get` and `put` operations.    
    check fileStream.close();

}

Client

The SFTP client is used to perform CRUD operation on remote files/directories using the SFTP protocol. This sample includes getting and putting file content with default configurations using the default port number.

For more information on the underlying module, see the FTP module.

import ballerina/ftp;
import ballerina/io;
import ballerina/lang.'string as strings;
public function main() returns error? {
    ftp:ClientConfiguration config = {
        protocol: ftp:SFTP,
        host: "sftp.example.com",
        port: 22,
        auth: {
            credentials: {username: "user1", password: "pass456"},

Creates the client with the connection parameters, host, username, and password. An error is returned in a failure. The default port number 22 for SSH is used with these configurations.

            privateKey: {
                path: "../resource/path/to/private.key",
                password: "keyPass123"
            }
        }
    };
    ftp:Client clientEp = check new(config);

Private key file location and its password (if encrypted) is given corresponding to the SSH key file used in the SFTP client.

    stream<byte[] & readonly, io:Error?> fileStream
        = check clientEp->get("/server/book.txt");
    check fileStream.forEach(isolated 
        function(byte[] & readonly fileContent) {
            io:println("File content received: "
                + checkpanic strings:fromBytes(fileContent));
        }
    );

Reads a file from a FTP server for a given file path. In error cases, an error is returned.

    stream<io:Block, io:Error?> bStream
        = check io:fileReadBlocksAsStream("/local/logFile.txt", 1024);
    check clientEp->put("/server", bStream);

Add a new file to the given file location. In error cases, an error is returned. The local file is provided as a stream of io:Block in which 1024 is the block size.

    check fileStream.close();

Closes the file stream to finish the get and put operations.

}
bal run sftp_client.bal
# File content of the received file would get printed.
# The newly-added file will appear in the SFTP server.