AWS的java.nio.file实现

时间:2016-12-13 03:05:13

标签: java azure amazon-s3 google-cloud-storage java.nio.file

java.nio.file是否有任何正式AWS实施?

我为GoogleCloudStorage here找到了一个,AWSAzure需要类似。

1 个答案:

答案 0 :(得分:1)

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

从Maven Central下载

<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);
}

如何在春季使用

添加到类路径并配置:

@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());
    }
}

注入任何弹簧组件:

@Autowired
private FileSystem s3FileSystem;
相关问题