SFTP Task

In Working with the Client API we briefly covered how to execute a command using the AbstractCommandTask. In the Maverick NG Client API tasks are how we perform operations over SSH and this includes SFTP. This article outlines the options available for the AbstractSftpTask.

For those of you familiar with the Maverick Legacy Client API you will find the SFTP task similar to the SftpClient class that previously implement SFTP.

The template for using the task is to place your task definition in your ConnectionStateListener implementations connect method. In this example we are downloading a file into the users home directory from their remote home directory. The default working directory for the SFTP task is always users home directory, as defined by the system property "user.home", not the applications current working directory.

    @Override
    public void connected(final Connection con) {
        con.addTask(new SftpClientTask(con) {	
	    @Override
		protected void doTask() {
		    try {
			get("tmp.dmg");
		    } catch (SftpStatusException
				| SshException
				| TransferCancelledException
				| FileNotFoundException e) {
			// Handle error
		    }
		}
	});
    }

 

To upload a file from the current file system you can:

    put("tmp.dmg");

You can also pass OutputStream to receive the file content without going to the local file system. 

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    get("tmp.dmg", out);

Or get an InputStream of the file content to process.

    InputStream in = getInputStream("tmp.dmg");

Similarly, you can pass an InputStream to write out data from your application held in an InputStream.

    InputStream in = ....
    put(in, "tmp.dmg");

Or get an OutputStream to write content to the remote system.

    OutputStream out = getOutputStream("tmp.dmg");

 

Changing Directory

 You can change the users local directory by calling the following inside the doTask method:

    lcd("/tmp");

Or change the remote directory using:

    cd("/opt");

Permissions

There are a number of methods for manipulating the permissions on the remote file system. Remember that unix permissions are normally stated in octal numbering system rather than decimal so any value passed should be preceded with zero to indicate this to the JVM.

    chmod(0644, "tmp.dmg");

You can change the owner or group of the file

    chown("501", "tmp.dmg");
chgrp("501", "tmp.dmg"); 

In SFTP version 3 or below the UID/GID expected is normally the numerical ID and not the user or group name. Version 4 or above supports names. The methods take a string to allow both to be passed. 

A default umask of 0666 ^ 0022 is applied to all new files and 0777 ^ 0022 for new directories. You can change the umask value using:

    umask(0006);

 

File Attributes 

You can get the current SftpFileAttributes of a file using a stat command.

    SftpFileAttributes attrs = stat("tmp.dmg")

Or the attributes of a link

    SftpFileAttributes attrs = statLink("tmp.dmg")

 If you want to change the attributes, get them using the above stat call and make modifications, then call

    setAttributes("tmp.dmg", attrs);

 

Moving and Deleting 

To move a file you rename it.

    rename("tmp.dmg", "new.dmg");

To delete a file use.

    rm("tmp.dmg");

 

 

 

 

 

 

 

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.