S3支持的java.io.File

huangapple go评论110阅读模式
英文:

S3-backed java.io.File

问题

以下是要翻译的内容:

这是我对于基于S3的Java File 的使用案例:

File videoFile = new File(OUTPUT_FOLDER + "channel" + channel + "-" + System.currentTimeMillis() + ".mp4");
AWTSequenceEncoder encoder = AWTSequenceEncoder.createSequenceEncoder(videoFile, Long.valueOf(FPS).intValue());
for (BufferedImage image : bufferedImages) {
    encoder.encodeImage(image);
}

上面只是一个使用案例,我想知道是否存在一种基于S3的File,可以作为java.io.File的即插即用替代方案;在上面的示例代码中,想法是将videoFile替换为直接持久化到S3或S3兼容存储,而不是持久化到磁盘。

那么最佳的注入方式是通过编译时还是运行时呢?

英文:

Here's my use case for a S3-backed Java File

File videoFile = new File(OUTPUT_FOLDER + "channel" + channel + "-" + System.currentTimeMillis() + ".mp4");
AWTSequenceEncoder encoder = AWTSequenceEncoder.createSequenceEncoder(videoFile, Long.valueOf(FPS).intValue());
 for (BufferedImage image : bufferedImages) {
      encoder.encodeImage(image);
}

Above is just one use-case, I am wondering if there exists a S3-backed File that can be used as a snap-in replacement for java.io.File; in the example code above the idea is to replace the videoFile of instead of persisting to disk, it will persist directly to S3 or S3-compatible storage.

And which is the best place to inject it, through compile-time or run-time?

答案1

得分: 1

据我所知,不存在这样的东西。

你所能做的最好的方式是在操作系统级别实现一个基于S3的文件系统。(例如,使用 s3backer linux man页面)如果文件系统具备正确的属性,使用 java.io.File 的应用程序将不会察觉到任何区别。

或者,你可以使用自定义的Java 7+ FileSystem javadoc,但是你需要修改你的代码,停止使用 File,开始使用更现代的API。

(当然,如果你愿意深入并在本地代码级别上修改JVM,任何事情都有可能。但这会引入一堆其他问题。)


那么将它注入的最佳位置是哪里,是通过编译时还是运行时?

那不是可行的选择。java.io.File 的重要部分是在JVM中以本地代码实现的,不可“插拔”或“注入”。

英文:

AFAIK, no such thing exists.

The best you could do would be to implement an S3-backed file system at the operating system level. (For example, using s3backer linux man page) If the file system had the right properties, applications that used java.io.File wouldn't know the difference.

Alternatively, use a custom Java 7+ FileSystem javadoc, but you will have to change your code to stop using File and start using the more modern APIs.

<sup>(Of course, if you were willing to dive in and modify the JVM at the native code level, anything would be possible. But then you are introducing a whole stack of other problems.)</sup>


> And which is the best place to inject it, through compile-time or run-time?

That is not a viable option. The important parts of java.io.File are implemented in native code in the JVM and are not "plugable" or "injectable".

答案2

得分: 1

没有一个直接替代 java.io.File 的 S3 插件,但有一个 FileSystemProvider SPI,可以将其作为 JVM 类路径上的提供程序使用,它允许您使用 java.nio.ChannelPath 来通过 URI 访问 S3 对象,就像访问文件一样。请查看 https://github.com/awslabs/aws-java-nio-spi-for-s3

英文:

There isn't a snap-in S3 replacement for java.io.File per se but there is a FileSystemProvider SPI that can be used as a provider on the JVM class path that will allow you to do things like use java.nio.Channels and Paths to access S3 objects via the URIs as if they were files. Take a look at https://github.com/awslabs/aws-java-nio-spi-for-s3

答案3

得分: 0

实际上存在一个 S3File(或者曾经存在)位于 https://packages.atlassian.com/maven/com/squeakysand/aws/squeakysand-aws-s3/

经过研究,作者曾是前 AWS 架构师,该项目现在似乎已被废弃,但许可证属于 Apache,因此一个良好的分支可能能够使其复苏。

至于回答何处最适合在 S3File注入,答案是编译时。然而,这可能对基本源代码产生相当大的干扰,如下所示:

S3File videoFile = new S3File(BUCKET_NAME, fileKey);
AWTSequenceEncoder encoder = AWTSequenceEncoder.createSequenceEncoder(videoFile, Long.valueOf(FPS).intValue());
for (BufferedImage image : bufferedImages) {
    encoder.encodeImage(image);
}

类似这样的代码会起作用。

实际上,S3File 应该具有与 java.io.File 完全相同的参数,这样可以将其用作 S3File videoFile = new S3File(fileName);

桶名称和其他连接已在构造函数中。

英文:

There actually exists an S3File (or was) from https://packages.atlassian.com/maven/com/squeakysand/aws/squeakysand-aws-s3/

Upon researching, the author is a previous AWS Architect, the project now seems to be abandoned, but the license is under Apache so a good fork might be able to revive it.

As for answering the best place to inject the S3File, the answer is compile-time. However, it might be quite intrusive to the base source code as such:

S3File videoFile = new S3File(BUCKET_NAME, fileKey);
AWTSequenceEncoder encoder = AWTSequenceEncoder.createSequenceEncoder(videoFile, Long.valueOf(FPS).intValue());
 for (BufferedImage image : bufferedImages) {
      encoder.encodeImage(image);
}

Something like this would work.

In fact, it should work with the S3File having the exact same parameters as of java.io.File so it can be used as S3File videoFile = new S3File(fileName);

Where the bucket name and other connection is already in the constructor.

huangapple
  • 本文由 发表于 2020年3月4日 09:19:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/60517797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定