【问题标题】:To upload a file to S3 bucket using SFTP Jsch library使用 SFTP Jsch 库将文件上传到 S3 存储桶
【发布时间】:2020-02-17 21:55:06
【问题描述】:

我在使用 Jsch 库的 SFTP 通道将文档上传到 S3 存储桶时遇到异常。我知道它在 Filezilla/cyberduck 中通过相同的密钥和细节工作,但不是 java。有人能指出我错过了什么吗?

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();
              }

       }
}

以下是例外情况

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)

【问题讨论】:

标签: java amazon-web-services amazon-s3 sftp jsch


【解决方案1】:

你必须使用 inputStream 从服务器获取文件并重新放入 就我而言,我使用了流动代码 我没有使用端口号进行身份验证:

private ChannelSftp setupJsch(String host, String username, String password) {

        JSch jsch = new JSch();
        //jsch.setKnownHosts(knownHosts);

        Session jschSession;
        try {
            jschSession = jsch.getSession(username, host);
            jschSession.setPassword(password);

            //extra config code
            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            jschSession.setConfig(config);


            jschSession.connect();
            return (ChannelSftp) jschSession.openChannel("sftp");
        } catch (JSchException e) {
        }
    return null;
    }   
private InputStream getInputStream(String host,
            String username, String password,String pdfFromIN) {
        InputStream in = null;
        ChannelSftp channelSftp;
        try {
            channelSftp = setupJsch(host, username, password);
            channelSftp.connect();
            in=channelSftp.get(pdfFromIN);
        } catch (JSchException |SftpException e) {

        }
        //channelSftp.exit();
        return in;
    }


    private void putFile(String host,
            String username, String password,InputStream in,String desFileRepo){

        ChannelSftp channelSftp;
        try {
            channelSftp = setupJsch(host, username, password);
            channelSftp.connect();
            channelSftp.put(in, desFileRepo);
            channelSftp.exit();
        } catch (JSchException |SftpException e) {
;

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 2021-06-20
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2018-02-07
    相关资源
    最近更新 更多