Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read an AWS S3 File with Java?

Tags:

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

Then I got an error:

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) 

Is there a way to open/read a file from AWS S3? Thanks a lot!

like image 619
Edamame Avatar asked Feb 17 '15 18:02

Edamame


People also ask

How do I read S3 file content in Java?

Steps to read S3 file in java can be: Create AmazonS3Client. Create S3Object using bucket name and key. Create buffer reader using S3Object and read file line by line.

How do I read data on Amazon S3?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.

Can you use Java with AWS?

Java on AWSSimplifies use of AWS services by providing a set of libraries that are consistent and familiar for Java developers. Use popular Integrated Development Environments (IDEs) to author, debug, and deploy your code on AWS. Use the AWS Cloud Development Kit (CDK) for your Infrastructure as Code with Java.


1 Answers

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(); 
like image 60
tedder42 Avatar answered Sep 27 '22 21:09

tedder42