我有一个问题,我想使用Java从Google Cloud Platform中的Debian服务器上使用Java上传数据(openoffice calc)并将其保存在数据库中,但是我无法做到这一点,因为我需要在我的Java代码中添加ssh密钥。
我如何将Java连接到Google Cloud Platform和Debian实例?我不知道该怎么做。
我有:
我的点子:
计算器→Java→SSH? →Google平台→实例→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();
}
}
}
解决方案如下:
如果我清楚地了解了您的情况,我认为您应该通过将数据发送到google云存储桶look here来实现。
还可以查看gcp中的服务帐户和凭据,在prem上使用它们真的很容易:)