将AWS s3文件读取为Java代码 [英] Read AWS s3 File to Java code
问题描述
我尝试将AWS s3中的文件读取到我的java代码中:
I tried to read a file from AWS s3 to my java code:
File file = new File("s3n://mybucket/myfile.txt");
FileInputStream fileInput = new FileInputStream(file);
然后我收到一个错误:
java.io.FileNotFoundException:s3n:/mybucket/myfile.txt(没有这样的文件或目录)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream。(FileInputStream .java:146)
java.io.FileNotFoundException: s3n:/mybucket/myfile.txt (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:146)
有没有办法从AWS s3打开/读取文件?非常感谢!
Is there a way to open/read a file from AWS s3? Thanks a lot!
推荐答案
Java中的'File'类不理解S3存在。 以下是从AWS文档中读取文件的示例:
The 'File' class from Java doesn't understand that S3 exists. Here's an example of reading a file from the AWS documentation:
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, key));
InputStream objectData = object.getObjectContent();
// Process the objectData stream.
objectData.close();
这篇关于将AWS s3文件读取为Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!