-
-
Save saswata-dutta/72a9d9d792848d4249114d847be8f4d9 to your computer and use it in GitHub Desktop.
Reading files from Amazon S3 directly in a java.net.URL object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment