AWS
是否有官方的java.nio.file
实现?
我为 GoogleCloudStorage
找到了一个 here ,AWS
和 Azure
也需要类似的功能。
最佳答案
您可以尝试使用 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/