因此,我正在尝试创建一个Java SFTP服务器,该服务器充当Apache S3存储桶的前端。您可以通过SFTP连接并管理存储桶中的S3文件,就像它们是SFTP服务器上的文件一样。
我已经将Apache MINA(v1.2.0)用作SFTP服务器,使用SftpSubsystemFactory
和默认值FileSystemFactory
(提供本地文件系统)可以正常工作。
我选择了Amazon-S3-FileSystem-NIO2(v1.3.0)作为FileSystem
,它使用了Apache AWS开发工具包,似乎是那里最好的选择。
public class S3FileSystemFactory implements FileSystemFactory {
private URI uri = URI.create("localhost");
public S3FileSystemFactory(URI uri){
this.uri = uri;
}
public FileSystem createFileSystem(Session session) throws IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
FileSystem s3FileSystem = FileSystems.newFileSystem(uri, new HashMap<String, Object>(), classLoader);
return s3FileSystem;
}
}
我只是将其设置FileSystemFactory
为MINA的
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setKeyPairProvider(buildHostKeyProviderFromFile(hostKeyType));
sshd.setPasswordAuthenticator(createPasswordAuthenticator());
sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE);
sshd.setSubsystemFactories(createSubsystemFactories());
sshd.setFileSystemFactory(createFileSystemFactory());
URI uri = URI.create("s3:///s3.amazonaws.com/my_bucket");
FileSystemFactory s3FileSystemFactory = new S3FileSystemFactory(uri);
sshd.setFileSystemFactory(s3FileSystemFactory);
我可以使用FileZilla / Command Line连接到该服务器,但是它会自动连接到ImageTransfer
存储桶(不是my_bucket
)。我可以导航到其他存储桶,甚至是子存储桶,但也无法显示内容,所有内容看起来都像一个空目录。
这可以通过FileSystem
我正在使用的方式完成,因为我可以列出目录内容,就像这样
Path p = s3FileSystem.getPath("/my_bucket");
String contents = "";
try (DirectoryStream<Path> stream = Files.newDirectoryStream(p, "*")) {
for (Path file : stream) {
contents += "\n \t - " + file.getFileName();
}
} catch (IOException | DirectoryIteratorException x) {}
我一直在浏览s3fs,MINA和AWS代码(由于文档非常有限),但无法查明此问题的根源。谁能告诉我我在做什么错?
记录中
打开所有库的日志记录后,我只看到一个问题
"HEAD
application/x-www-form-urlencoded; charset=utf-8
Fri, 20 May 2016 09:58:07 GMT
/MYURL/."
2016-05-20 10:58:07.240 DEBUG 13323 --- [system-thread-1] c.a.http.impl.client.SdkHttpClient : Stale connection check
2016-05-20 10:58:07.243 DEBUG 13323 --- [system-thread-1] c.a.http.impl.client.SdkHttpClient : Attempt 1 to execute request
2016-05-20 10:58:07.434 DEBUG 13323 --- [system-thread-1] c.a.http.impl.client.SdkHttpClient : Connection can be kept alive indefinitely
2016-05-20 10:58:07.435 DEBUG 13323 --- [system-thread-1] com.amazonaws.request : Received error response: com.amazonaws.services.s3.model.AmazonS3Exception: Not Found (Service: null; Status Code: 404; Error Code: 404 Not Found; Request ID: MYREQID), S3 Extended Request ID: MYEXTREQID
问题是双重的。
首先,Apache的MINA SSHD要求的permission
属性是本任何FileSystem
它使用,这是很奇怪,因为这是如此明显一POSIX特定属性亚马逊-S3-文件系统NIO2没有提供它。
其次,Apache MINA SSHD在S3FileSystemProvider中调用(并要求)一个未实现的方法,该方法是FileChannel
public FileChannel newFileChannel(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs)
一个有问题的解决方案是将POSIX读/写权限硬编码到返回的S3属性中,并创建一个S3FileChannel
简单地调用现有S3SeekableByteChannel上的方法的。
这是我目前能想到的最好的解决方案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句