Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PUBDEV-3321] Allow arbitrary S3 connection end point/region. #164

Merged
merged 3 commits into from
Oct 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 40 additions & 4 deletions h2o-persist-s3/src/main/java/water/persist/PersistS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import com.amazonaws.*;
import com.amazonaws.auth.*;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.*;
import com.amazonaws.regions.Region;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.*;

import com.google.common.io.ByteStreams;
Expand All @@ -39,7 +42,7 @@ public static AmazonS3 getClient() {
try {
H2OAWSCredentialsProviderChain c = new H2OAWSCredentialsProviderChain();
ClientConfiguration cc = s3ClientCfg();
_s3 = new AmazonS3Client(c, cc);
_s3 = configureClient(new AmazonS3Client(c, cc));
} catch( Throwable e ) {
e.printStackTrace();
StringBuilder msg = new StringBuilder();
Expand Down Expand Up @@ -117,16 +120,19 @@ public static InputStream openStream(Key k, RIStream.ProgressMonitor pmon) throw
return new H2SO3InputStream(k, pmon);
}

public static Key loadKey(S3ObjectSummary obj) throws IOException {
return S3FileVec.make(encodePath(obj.getBucketName(), obj.getKey()),obj.getSize());
public static Key loadKey(ObjectListing listing, S3ObjectSummary obj) throws IOException {
// Note: Some of S3 implementations does not fill bucketName of returned object (for example, Minio).
// So guess it based on returned ObjectListing
String bucketName = obj.getBucketName() == null ? listing.getBucketName() : obj.getBucketName();
return S3FileVec.make(encodePath(bucketName, obj.getKey()),obj.getSize());
}


private static void processListing(ObjectListing listing, ArrayList<String> succ, ArrayList<String> fail, boolean doImport){
for( S3ObjectSummary obj : listing.getObjectSummaries() ) {
try {
if (doImport) {
Key k = loadKey(obj);
Key k = loadKey(listing, obj);
succ.add(k.toString());
} else {
succ.add(obj.getKey());
Expand Down Expand Up @@ -299,6 +305,15 @@ private static ObjectMetadata getObjectMetadataForKey(Key k) {
public final static String S3_MAX_HTTP_CONNECTIONS_PROP = SYSTEM_PROP_PREFIX + "persist.s3.maxHttpConnections";
/** S3 force HTTP traffic */
public final static String S3_FORCE_HTTP = SYSTEM_PROP_PREFIX + "persist.s3.force.http";
/** S3 end-point, for example: "https://localhost:9000 */
public final static String S3_END_POINT = SYSTEM_PROP_PREFIX + "persist.s3.endPoint";
/** S3 region, for example "us-east-1",
* see {@link com.amazonaws.regions.Region#getRegion(com.amazonaws.regions.Regions)} for region list */
public final static String S3_REGION = SYSTEM_PROP_PREFIX + "persist.s3.region";
/** Enable S3 path style access via setting the property to true.
* See: {@link com.amazonaws.services.s3.S3ClientOptions#setPathStyleAccess(boolean)} */
public final static String S3_ENABLE_PATH_STYLE = SYSTEM_PROP_PREFIX + "persist.s3.enable.path.style";


static ClientConfiguration s3ClientCfg() {
ClientConfiguration cfg = new ClientConfiguration();
Expand All @@ -312,6 +327,27 @@ static ClientConfiguration s3ClientCfg() {
return cfg;
}

static AmazonS3Client configureClient(AmazonS3Client s3Client) {
if (System.getProperty(S3_REGION) != null) {
String region = System.getProperty(S3_REGION);
Log.debug("S3 region specified: ", region);
s3Client.setRegion(RegionUtils.getRegion(region));
}
// Region overrides end-point settings
if (System.getProperty(S3_END_POINT) != null) {
String endPoint = System.getProperty(S3_END_POINT);
Log.debug("S3 endpoint specified: ", endPoint);
s3Client.setEndpoint(endPoint);
}
if (System.getProperty(S3_ENABLE_PATH_STYLE) != null && Boolean.valueOf(System.getProperty(S3_ENABLE_PATH_STYLE))) {
Log.debug("S3 path style access enabled");
S3ClientOptions sco = new S3ClientOptions();
sco.setPathStyleAccess(true);
s3Client.setS3ClientOptions(sco);
}
return s3Client;
}

@Override public void delete(Value v) {
throw new UnsupportedOperationException();
}
Expand Down