【问题标题】:Java DataReading Google Cloud PlatformJava数据读取谷歌云平台
【发布时间】:2018-01-10 18:56:48
【问题描述】:

我有一个问题,我想在 Google Cloud Platform 中使用我的 debian 服务器上的 java 上传数据(openoffice calc)并将其保存在我的数据库中,但我不能这样做,因为我需要将 ssh 密钥添加到我的java代码。

如何将 java 连接到 Google Cloud Platform 和 debian 实例?我不知道该怎么做。

我有:

  • Windows 10 上的 Java 7。
  • Debian 服务器 java 7
  • 带有 assh 密钥的 Google Cloud Platform。

我的想法: 计算 → java → ssh? → 谷歌平台 → 实例 → java → 数据库

我使用的是 maven 仓库 JSch:

    public static void main(String[] args) {

        /*Below we have declared and defined the SFTP HOST, PORT, USER 
                   and Local private key from where you will make connection */
        String SFTPHOST = "ip";
        int    SFTPPORT = 22;
        String SFTPUSER = "user";
        // this file can be id_rsa or id_dsa based on which algorithm is used to create the key
        String privateKey = "file"; 
        String SFTPWORKINGDIR = "folder";

        JSch jSch = new JSch();
        Session     session     = null;
        Channel     channel     = null;
        ChannelSftp channelSftp = null;
        try {
            jSch.addIdentity(privateKey);
            System.out.println("Private Key Added.");
            session = jSch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
            System.out.println("session created.");

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            System.out.println("shell channel connected....");
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(SFTPWORKINGDIR);
            System.out.println("Changed the directory...");
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SftpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(channelSftp!=null){
                channelSftp.disconnect();
                channelSftp.exit();
            }
            if(channel!=null) channel.disconnect();

            if(session!=null) session.disconnect();
        }
    }

}

【问题讨论】:

    标签: java google-app-engine ssh datareader calc


    【解决方案1】:

    如果我清楚地理解了你的场景,我认为你应该通过将数据发送到谷歌云存储桶来做到这一点:look here

    还可以查看 gcp 中的服务帐户和凭据,在 prem 中使用它们真的很容易:)

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 2020-06-02
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 2020-08-06
      • 2018-09-26
      相关资源
      最近更新 更多