Home » Java » java – To upload a file to S3 bucket using SFTP Jsch library-Exceptionshub

java – To upload a file to S3 bucket using SFTP Jsch library-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am getting exception while uploading the document using SFTP channel using jsch library to S3 bucket. I know its working through the same key and details in Filezilla/cyberduck but not java. can someone point out what am i missing?

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class TestSSHUpdloadFile {
    public static void main(String[] args) throws Exception {

        JSch jsch = new JSch();
        String privateKey = "/Users/Documents/SFTP/sftprivate_key";
              Session session = null;
              try {
                  jsch.addIdentity(privateKey, "Passphrase");
                  session = jsch.getSession("User", "sftp.aws.com", 22);
                  session.setConfig("StrictHostKeyChecking", "no");
                  //session.setPassword("Passphrase");
                  session.connect();

                  Channel channel = session.openChannel("sftp");
                  channel.connect();
                  ChannelSftp sftpChannel = (ChannelSftp) channel;
                  sftpChannel.put("/Users/Documents/Temp1.csv", "/mys3bucket/Temp1.csv");  
                  sftpChannel.exit();
                  session.disconnect();
              } catch (JSchException e) {
                  e.printStackTrace();  
              } catch (SftpException e) {
                  e.printStackTrace();
              }

       }
}

Below is the exception

com.jcraft.jsch.JSchException: USERAUTH fail
    at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:119)
    at com.jcraft.jsch.Session.connect(Session.java:470)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at TestS3.TestSSHUpdloadFile.main(TestSSHUpdloadFile.java:21)
How to&Answers: