Best way to read a file from Amazon S3 in Java

I am assigned an S3 file url in the following format:

S3N: // XXXXXXXXXXXXXXXXXXXXXX: XXXXXXXXXXXXXXXXXXXXXXXXXXXX @ bucket /foo/bar.json

What is the best way to pull a json file from this location?

I would like to treat the URL as an opaque value and avoid parsing it so that it changes and I needed to change the underlying code.

When I try to pass java.net.URL value it throws Mal Formed Exception because it doesn't recognize S3 protocol.

Thank!

+3


source to share


2 answers


You can implement URLStreamHandler which supports s3n protocol.

The Java doc URL constructor explains how the URLStreamHandle is detected.



In the URLStreamHandler, you will need to parse the URL in order to map it to the s3 client.

But any code using it should not depend on any S3 related element and should work with a different store, if necessary, the URLStreamHandler is present.

+2


source


In Amazon S3

you first need to authenticate with a given bucket, and then pull out the InputStream

desired object, and then you can do your job.



To pull an object through Amazon RestWebService to get the object or access the object through the url required to give it public access, See here

0


source







All Articles