Workaround for unsupported SETSTAT request on SFTP server with sshj

9.7k Views Asked by At

I'm trying to SFTP to a server using identity string: SSH-2.0-AWS_SFTP_1.0 with the following Java code using sshj.

<dependency>
    <groupId>com.hierynomus</groupId>
    <artifactId>sshj</artifactId>
    <version>0.29.0</version>
</dependency>
private SSHClient setupSshj(String remoteHost, String username, String password) throws IOException {
    SSHClient client = new SSHClient();
    client.addHostKeyVerifier(new PromiscuousVerifier());
    client.connect(remoteHost);
    client.authPassword(username, password);
    return client;
}

public void sftpfiles() throws IOException {
    if (Boolean.parseBoolean(GetConfigValue("dds", "sendFiles"))) {
        SSHClient sshClient = setupSshj(GetConfigValue("dds", "RemoteAddress"), GetConfigValue("dds", "RemoteLogin"), GetConfigValue("dds", "RemotePassword"));
        SFTPClient sftpClient = sshClient.newSFTPClient();
        sftpClient.put("/home/vm/test.txt", GetConfigValue("dds", "RemoteDirectory"));
        sftpClient.close();
        sshClients.disconnect();
    }
}

and get the error

Error SETSTAT unsupported

I understand that the AWS service doesn’t allow setting timestamps when uploading however I don't know what adjustments are required in order to configure the SFTP client.

3

There are 3 best solutions below

0
On BEST ANSWER

It seems that sshj SSHClient API does not allow preventing the use of the SETSTAT request. You will have to use more low level API, like SFTPFileTransfer:

SFTPEngine engine = new SFTPEngine(sshClient).init();
SFTPFileTransfer xfer = new SFTPFileTransfer(engine);
xfer.setPreserveAttributes(false);
xfer.upload("/home/vm/test.txt", GetConfigValue("dds", "RemoteDirectory"));
1
On

By default the preserveAttributes of SFTPFileTransfer is true

 [private volatile boolean preserveAttributes = true;]

Hence, setting it to false for AWS services would stop preserving data during uploads.

0
On

SSHJ does support skipping SETSTAT method.

sftpClient.getFileTransfer().setPreserveAttributes(false)