我想使用JSch或脚本通过Java中的SFTP将文件从存储桶GCP传输到远程服务器,而不是本地目录。
try{
JSch jsch = new JSch();
jsch.addIdentity(privateSftpKey);
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
log.info("Host connected.");
channel = session.openChannel("sftp");
channel.connect();
log.info("sftp channel opened and connected.");
channelSftp = (ChannelSftp) channel;
String sftpDirectory = "/home/share";
File directory = new File("C:\\Users\\XYZ\\Desktop\\Learning\\Projects\\TransferStorageBucketToRemoteServer");
File[] fList = directory.listFiles();
for (File file : fList){
if (file.isFile()){
String filename=file.getAbsolutePath();
channelSftp.put(filename, sftpDirectory, ChannelSftp.OVERWRITE);
System.out.println(filename + " transferred to " + sftpDirectory );
}
}
log.info("File transfered successfully to host.");
} catch (Exception ex) {
log.info("Exception found while tranfer the response.");
log.info("Exception Message...: {}",ex.getMessage());
}
当我在互联网上搜索时,仅从中传输文件
google cloud storage API代码,从存储桶中读取文件
Credentials credentials = GoogleCredentials
.fromStream(new FileInputStream(jsonKey));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials)
.setProjectId("projectId").build().getService();
Blob blob = storage.get("bucket-name", "file.txt");
ReadChannel readChannel = blob.reader();
但是不确定如何直接从GCP ReadChannel在SFTP通道中上传而不将其写入另一个输出文件,然后在SFTP通道中传输它
channelSftp.put(file_from_readChannel , sftpDirectory, ChannelSftp.OVERWRITE);
但是我看不到从存储桶直接转移到远程服务器。
在Java或命令中,任何人都可以协助解决此问题以及可行的方法吗?
答案 0 :(得分:1)
当它在Kubernetes上运行时,一种可行的方法是将存储桶作为驱动器安装在Pod上,然后将文件上传到FTP服务器,就像它们是本地文件一样。
要将桶作为驱动器安装在pu上,可以使用GCS保险丝,here是一个示例。
这可以通过在Dockerfile上添加它来实现:
Dockerfile
RUN apt-get update && apt-get install --yes --no-install-recommends \
ca-certificates \
curl \
gnupg \
&& echo "deb http://packages.cloud.google.com/apt $GCSFUSE_REPO main" \
| tee /etc/apt/sources.list.d/gcsfuse.list \
&& curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - \
&& apt-get update \
&& apt-get install --yes gcsfuse \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN mkdir <MOUNTING_POINT>
RUN gcsfuse -o nonempty <BUCKET_NAME> <MOUNTING_POINT>
一旦挂载,您将可以upload the files to the SFTP,就像它们是本地文件一样。