FTP

FTP Server

docker-compose.yml

version: "3"
services:
  nats:
    image: atmoz/sftp
    container_name: sftp-server
    command: foo:pass:1001
    volumes:
      - "./upload:/home/foo/upload"
    ports:
      - "2222:22"
$ docker-compose -f docker-compose.yml up

SFTP Listener

sftp_listener_credentials_auth.bal

import ballerina/ftp;
import ballerina/io;

listener ftp:Listener securedEP = new ({
    protocol: ftp:SFTP,
    host: "127.0.0.1",
    port: 2222,
    auth: {
        credentials: {
            username: "foo",
            password: "pass"
        }
    },
    path: "/upload",
    pollingInterval: 2,
    fileNamePattern: "(.*).txt"
});

isolated service on securedEP {
    isolated function onFileChange(ftp:WatchEvent event) {
        foreach ftp:FileInfo addedFile in event.addedFiles {
            io:println("Added file of path: ", addedFile.path);
        }
        foreach string deletedFile in event.deletedFiles {
            io:println("Deleted file of path: ", deletedFile);
        }
    }
}
$ bal run sftp_listener_credentials_auth.bal

SFTP Client

sftp_client_credentials_auth.bal

import ballerina/ftp;
import ballerina/io;

final ftp:Client securedEP = check new ({
    protocol: ftp:SFTP,
    host: "127.0.0.1",
    port: 2222,
    auth: {
        credentials: {
            username: "foo",
            password: "pass"
        }
    }
});

public function main() returns error? {
    ftp:FileInfo[] response = check securedEP->list("/upload");
    foreach ftp:FileInfo info in response {
        io:println("File: ", info.path);
    }
}
$ bal run sftp_client_credentials_auth.bal

Resources

private.key

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCBXKLp9WJUuJko
SfDn3HM2LWVxiHrP11me6D4X2AqoUiCZ1w976q1LuBlzWNhONYwJvHXewUuRmo0d
4NRp2ma0qjdMN246XWyozpzPfJdvovfiT+V6HnSbMg82ZppfF3e85Cgrg6mSA7Wf
faaHr62SnexseZ9ioNCuRUrW2Zg208l99IItP4K4iGclQlyjs3S6drH7JJk3rv8B
f76wmmoCEAhknOtGLq543dEUIjFgNdIy/GsSY52fiA4LSjUCv27kGn03nayqoZs3
XHqM0qLdt9SPXbY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