2

我需要监控 Amazon S3 上的目录以检查是否有任何新文件添加到该目录。我尝试使用 Java NIO Watch Service,但它无法正常工作。如果我在提供的 S3 路径中使用以下语法:

String pathToMonitor="file://https://abc/dir";  //Line1
Path path=Paths.get(new URL(pathToMonitor).toURI()); //Line2
Boolean isFolder=(Boolean) Files.getAttribute(file, "basic:isDirectory", LinkOption.NOFOLLOW_LINKS); //Line3

然后我收到以下错误:

java.nio.file.FileSystemException: \\https\abc\dir: The network path was not found.

at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:53)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:38)
at sun.nio.fs.AbstractBasicFileAttributeView.readAttributes(AbstractBasicFileAttributeView.java:168)
at sun.nio.fs.AbstractFileSystemProvider.readAttributes(AbstractFileSystemProvider.java:92)
at java.nio.file.Files.readAttributes(Files.java:1961)
at java.nio.file.Files.getAttribute(Files.java:1866)

如果我file://从路径中删除前缀,则会生成以下错误:

Exception in thread "main" java.nio.file.FileSystemNotFoundException: Provider "https" not installed
at java.nio.file.Paths.get(Paths.java:147)

如果我将“Line2”修改为,Path path=Paths.get("https://abc/dir");则生成以下跟踪:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 5: https://abc/dir
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.nio.file.Paths.get(Paths.java:84)

请让我知道我在这里做错了什么以及是否可以使用 Java 监视服务来监视此类 Web 资源,或者是否有任何其他框架/api。

谢谢

4

1 回答 1

3

You've given it a mixture of a file protocol and an http protocol, which doesn't really make sense.

Basically you can't do what you're trying to do, if the only way you have access to the resource is via HTTP. There's no general mechanism for being notified when an HTTP resource changes, because there's no general mechanism for having push notifications from an HTTP resource, and it's outside the operating system's control so it can't intercept changes as they happen. With a local file, your operating system can detect changes as they happen because it's ultimately responsible for dealing with writes to local disk, but that doesn't apply in your situation.

You'd need something that polls for changes, unless S3 has something bespoke in place to push change notifications (but that's something you'd have to investigate separately). You can't do it with Java NIO.

于 2014-12-01T10:52:53.503 回答