我试图使用Apache VFS将一个压缩文件上传到远程FTP服务器。执行环境是AWS Java 8 Lambda(如果相关的话)。这是我目前的实现,它大致遵循所提供的例子。此处:
public static FileSystemOptions createDefaultOptions()
throws FileSystemException {
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
return opts;
}
public void uploadFile(File file) throws IOException {
StandardFileSystemManager fsManager = new StandardFileSystemManager();
fsManager.init();
FileObject localFile = fsManager.resolveFile(file.getAbsolutePath());
FileObject remote = fsManager.resolveFile(
"sftp://<USERNAME>:<PASSWORD>@<DOMAINNAME>.com/tmp1.zip");
remote.copyFrom(localFile, Selectors.SELECT_SELF);
remote.close();
}
当我用一个我确认存在于Lambda中的定义良好的文件调用这个方法时,我得到了一个错误。
event_description="Could not find file with URI
"sftp://<USERNAME>:<PASSWORD>@<DOMAINNAME>.com/tmp1.zip"
because it is a relative path, and no base URI was provided."
我可以在终端上用同样的凭证通过FTP连接到服务器,所以我不认为这与这些有关。另一个我看过的问题可能与此有关,可以在这里找到。此处虽然我已经实施了它的建议,但还是得到了同样的 “无基础URI “错误。
解决方案:
当你在VFS中通过SFTP解析远程文件时,你应该提供SftpFileSystemOptions与SftpFileSystemConfigs.所以,而不是。
FileObject remote = fsManager.resolveFile("sftp://<USERNAME>:<PASSWORD>@<DOMAINNAME>.com/tmp1.zip");
尝试。
try {
FileObject remote = fsManager.resolveFile("sftp://<USERNAME>:<PASSWORD>@<DOMAINNAME>.com/tmp1.zip",createDefaultOptions());
} catch(FileSystemException e) {
e.printStackTrace();
}
你可能需要通过trycatch或使用方法签名抛出异常来处理FileSystemException。
本文来自投稿,不代表运维实战侠立场,如若转载,请注明出处:https://www.shizhanxia.com/893.html