Skip to content

Instantly share code, notes, and snippets.

@lucastex
Created April 13, 2011 17:38
Show Gist options
  • Save lucastex/917988 to your computer and use it in GitHub Desktop.
Save lucastex/917988 to your computer and use it in GitHub Desktop.
Reading files from Amazon S3 directly in a java.net.URL object.
package sun.net.www.protocol.s3;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import org.jets3t.service.ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;
public class Handler extends URLStreamHandler {
protected URLConnection openConnection(URL url) throws IOException {
return new URLConnection(url) {
@Override
public InputStream getInputStream() throws IOException {
//aws credentials
String accessKey = null;
String secretKey = null;
if (url.getUserInfo() != null) {
String[] credentials = url.getUserInfo().split("[:]");
accessKey = credentials[0];
secretKey = credentials[1];
}
//bucket
String bucket = url.getHost().substring(0, url.getHost().indexOf("."));
//key
String key = url.getPath().substring(1);
try {
RestS3Service s3Service = new RestS3Service(new AWSCredentials(accessKey, secretKey));
S3Object s3obj = s3Service.getObject(bucket, key);
return s3obj.getDataInputStream();
} catch (ServiceException e) {
throw new IOException(e);
}
}
@Override
public void connect() throws IOException { }
};
}
}
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
public class TestUrl {
public static void main(String[] args) throws IOException {
URL url = new URL("s3://<access-key>:<secret-key>@<bucket>.s3.amazonaws.com/<filename>");
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
System.out.println("connected");
System.out.println(writer.toString());
} else {
System.out.println("no-connection");
}
}
}
@srinukhandavalli
Copy link

Hi

While compiling this code i am getting Exception as below.

Exception in thread "main" java.net.MalformedURLException: unknown protocol: s3
at java.net.URL.(Unknown Source)
at java.net.URL.(Unknown Source)
at java.net.URL.(Unknown Source)
at com.task.TestUrl.main(TestUrl.java:24)

Any help ?

Thanks

@BDF
Copy link

BDF commented Mar 26, 2016

@srinukhandavalli
http://stackoverflow.com/questions/861500/url-to-load-resources-from-the-classpath-in-java
Should answer your questions.

A note about the code:
It is a terrible idea to put the access-key and secret-key directly into a URL. WAY too much risk of someone letting those values leak or be exposed in an unintended way. A much better way is to rely on Amazon's DefaultAWSCredentialsProviderChain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment