如何使用 Java 读取 AWS S3 文件? [英] How can I read an AWS S3 File with Java?
本文介绍了如何使用 Java 读取 AWS S3 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试从 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 (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(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();
这篇关于如何使用 Java 读取 AWS S3 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文