AWS 的 java.nio.file 实现

标签 java amazon-s3 java.nio.file s3fs-nio

AWS 是否有官方的java.nio.file 实现?

我为 GoogleCloudStorage 找到了一个 hereAWSAzure 也需要类似的功能。

最佳答案

您可以尝试使用 Amazon AWS S3 FileSystem Provider JSR-203 for Java 7 (NIO2)

从 Maven 中心下载

<dependency>
    <groupId>com.upplication</groupId>
    <artifactId>s3fs</artifactId>
    <version>2.2.2</version>
</dependency>

添加到您的 META-INF/services/java.nio.file.spi.FileSystemProvider(如果尚不存在则创建)像这样的新行:com.upplication.s3fs.S3FileSystemProvider。

使用此代码创建文件系统并设置为具体端点。

FileSystems.newFileSystem("s3:///", new HashMap<String,Object>(), Thread.currentThread().getContextClassLoader());

如何在 Apache MINA 中使用

public FileSystemFactory createFileSystemFactory(String bucketName) throws IOException, URISyntaxException {
    FileSystem fileSystem = FileSystems.newFileSystem(new URI("s3:///"), env, Thread.currentThread().getContextClassLoader());
    String bucketPath = fileSystem.getPath("/" + bucketName);

    return new VirtualFileSystemFactory(bucketPath);
}

在Spring中如何使用

添加到类路径并配置:

@Configuration
public class AwsConfig {

    @Value("${upplication.aws.accessKey}")
    private String accessKey;

    @Value("${upplication.aws.secretKey}")
    private String secretKey;

    @Bean
    public FileSystem s3FileSystem() throws IOException {
        Map<String, String> env = new HashMap<>();
        env.put(com.upplication.s3fs.AmazonS3Factory.ACCESS_KEY, accessKey);
        env.put(com.upplication.s3fs.AmazonS3Factory.SECRET_KEY, secretKey);

        return FileSystems.newFileSystem(URI.create("s3:///"), env, Thread.currentThread().getContextClassLoader());
    }
}

注入(inject)任何 Spring 组件:

@Autowired
private FileSystem s3FileSystem;

关于AWS 的 java.nio.file 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113119/

相关文章:

java - 在 SpringBoot 2.0.1.RELEASE 应用程序中读取文件

java - 如何使用分页和 spring data jpa 获取 findAll() 服务的所有记录?

php - Nginx PHP 因大文件上传(超过 6 GB)而失败

javascript - 使用 AWS SDK for JavaScript 访问 AWS S3

java - 当目标路径是目录时,Files.newInputStream() 的无关紧要的行为?

linux - Java.nio.Files "createSymbolicLink"删除目标目录内容?

java - 具有多个参数的 EasyMock 构造函数

java - 为什么 setFollowRedirects(boolean auto) 是 HttpURLConnection 的静态方法

java - FluentIterable : should limit() be called as soon as possible?

regex - Glob 模式与亚马逊 s3