This object supports the client side of the sftp protocol, version 3, and implements an interface similar to the usual sftp command line client.

1 SftpClient

SFTP Object can be created invoking a new Ax.net.SFTP Constructor. Parameters expected are: hostname, port, username and password.

Copy
<script>
    var sftpconn = new Ax.net.SftpClient(192.168.10.11, 22, 'informix','******');
</script>
Return Method Description
void disconnect() Disconnects from server
boolean exists(String name) Returns true if file name exists
void cd(String name) Changes working directory
void rm(String name) Deletes file
void rmdir(String name) Deletes empty directory
ArrayList<String> list(String pattern) Returns an array of files or folder names. You can get only a subset of files applying a pattern. You can use * as a replacement for any number of characters.
ArrayList<LsEntry> listFiles(String pattern) Return an array of LsEntry objects
void mkdir(String name) Creates a new directory with name passed as parameter
void put(String name, Object data) Sends the content of data and creates a new file remotely with this content. data can be a Blob, file, byte[], etc.
String pwd() Returns the current working directory
void rename(String src, String dst) Renames a file in server.
byte[] getBytes(String name) gets content of remote file in server with name passed as parameter.
Blob getBlob(String name) gets content of remote file and returns a Blob. Internal blob name are set from remote file name passed as parameter.

2 SFTP Usage

.

2.1 Basic example

.

Copy
<script>
    var sftpcon = new Ax.net.SftpClient('dbsrv4', 22, 'informix','*****');

    sftpcon.cd("/tmp");
    if (!sftpcon.exists("TestFolder")) {
        sftpcon.mkdir("TestFolder");
    }
    sftpcon.cd("TestFolder");
    console.log(sftpcon.pwd());
    sftpcon.put("RedSprites_Broady_960.jpg", new Ax.net.URL("https://apod.nasa.gov/apod/image/1902/RedSprites_Broady_960.jpg").getBytes());

    return sftpcon.getBlob("RedSprites_Broady_960.jpg");
</script>

2.2 Advanced example

This advanced example, shows how an almost real example, creating folders, sending files, procesing directory or file lists and moving processed files to other directory.

Copy
<script>
    var sftpcon = new Ax.net.SftpClient('dbsrv4', 22, 'informix','*****');

    if (!sftpcon.exists("/tmp/TestFolder")) {
        console.log("TestFolder doesn't exists. Creating...");
        sftpcon.mkdir("/tmp/TestFolder");
        sftpcon.mkdir("/tmp/TestFolder/PROCESSED/");
    }
    sftpcon.cd("/tmp/TestFolder");
    console.log(sftpcon.pwd());

    sftpcon.put("RedSprites_Broady_960.jpg", new Ax.net.URL("https://apod.nasa.gov/apod/image/1902/RedSprites_Broady_960.jpg").getBytes());
    sftpcon.put("M82Magnet_SOFIA_960.jpg", new Ax.net.URL("https://apod.nasa.gov/apod/image/1903/M82Magnet_SOFIA_960.jpg").getBytes());
    sftpcon.put("moonrisemk_connelley_960.jpg", new Ax.net.URL("https://apod.nasa.gov/apod/image/1903/moonrisemk_connelley_960.jpg").getBytes());
    sftpcon.put("M78_RGBwright1024.jpg", new Ax.net.URL("https://apod.nasa.gov/apod/image/1903/M78_RGBwright1024.jpg").getBytes());
    
    console.log(sftpcon.list("/tmp/TestFolder/M*.jpg"));
    console.log(sftpcon.list("/tmp/TestFolder/"));
    console.log(sftpcon.list("*.jpg"));
    console.log(sftpcon.list("FILENOTFOUND.TXT"));

    for each (var f of sftpcon.listFiles("/tmp/TestFolder/M*.jpg")) {
        console.log(f.getFilename() + " = " + f.getLongname() + "  Attr:" + f.getAttrs());
        sftpcon.rename(f.getFilename(), "PROCESSED/" + f.getFilename());
    }

    return sftpcon.getBlob("/tmp/TestFolder/PROCESSED/M78_RGBwright1024.jpg");
</script>
/tmp/TestFolder
[[M78_RGBwright1024.jpg, M82Magnet_SOFIA_960.jpg]]
[[moonrisemk_connelley_960.jpg, PROCESSED, ., M78_RGBwright1024.jpg, .., RedSprites_Broady_960.jpg, M82Magnet_SOFIA_960.jpg]]
[[moonrisemk_connelley_960.jpg, M78_RGBwright1024.jpg, RedSprites_Broady_960.jpg, M82Magnet_SOFIA_960.jpg]]
[[]]
M78_RGBwright1024.jpg = -rw-rw-r--    1 informix informix   319827 Mar 12 17:38 M78_RGBwright1024.jpgAttr:-rw-rw-r-- 1000 1000 319827 Tue Mar 12 17:38:15 CET 2019
M82Magnet_SOFIA_960.jpg = -rw-rw-r--    1 informix informix    34520 Mar 12 17:38 M82Magnet_SOFIA_960.jpgAttr:-rw-rw-r-- 1000 1000 34520 Tue Mar 12 17:38:11 CET 2019