diff --git a/source/java/src/org/lucee/extension/resource/s3/AmazonS3Client.java b/source/java/src/org/lucee/extension/resource/s3/AmazonS3Client.java index 078c3d0..cd90934 100644 --- a/source/java/src/org/lucee/extension/resource/s3/AmazonS3Client.java +++ b/source/java/src/org/lucee/extension/resource/s3/AmazonS3Client.java @@ -53,10 +53,17 @@ public class AmazonS3Client implements AmazonS3 { public static AmazonS3Client get(String accessKeyId, String secretAccessKey, String host, org.lucee.extension.resource.s3.region.RegionFactory.Region region, long liveTimeout, boolean pathStyleAccess, Log log) throws S3Exception { + String key = accessKeyId + ":" + secretAccessKey + ":" + host + ":" + (region == null ? "default-region" : S3.toString(region)) + ":" + pathStyleAccess; AmazonS3Client client = pool.get(key); if (client == null || client.isExpired()) { - pool.put(key, client = new AmazonS3Client(accessKeyId, secretAccessKey, host, region, key, liveTimeout, pathStyleAccess, log)); + synchronized (pool) { + client = pool.get(key); + if (client == null || client.isExpired()) { + pool.put(key, client = new AmazonS3Client(accessKeyId, secretAccessKey, host, region, key, liveTimeout, pathStyleAccess, log)); + } + } + } return client; } diff --git a/source/java/src/org/lucee/extension/resource/s3/S3.java b/source/java/src/org/lucee/extension/resource/s3/S3.java index 31574a0..a8113ab 100755 --- a/source/java/src/org/lucee/extension/resource/s3/S3.java +++ b/source/java/src/org/lucee/extension/resource/s3/S3.java @@ -152,12 +152,12 @@ public S3(S3Properties props, long cacheTimeout, long liveTimeout, boolean cache defaultRegion = props.getDefaultLocation(); } } + defaultRegion = S3Util.extractLocationFromHostIfNecessary(defaultRegion, host); if (cacheRegions) { new CacheRegions().start(); } this.log = log; - } public String getHost() { @@ -2209,7 +2209,7 @@ public Region toRegion(String bucketName, String strRegion) throws S3Exception { else if (!Util.isEmpty(bucketName)) { return getBucketRegion(bucketName, true); } - return null; + return Util.isEmpty(defaultRegion, true) ? null : RegionFactory.getInstance(defaultRegion); } public static String toString(Region region) { diff --git a/source/java/src/org/lucee/extension/resource/s3/S3Properties.java b/source/java/src/org/lucee/extension/resource/s3/S3Properties.java index c7ea2af..3323602 100644 --- a/source/java/src/org/lucee/extension/resource/s3/S3Properties.java +++ b/source/java/src/org/lucee/extension/resource/s3/S3Properties.java @@ -67,6 +67,7 @@ public void setDefaultLocation(String defaultLocation) { } public String getDefaultLocation() { + if (defaultLocation == null) this.defaultLocation = S3Util.extractLocationFromHostIfNecessary(this.defaultLocation, this.host); return defaultLocation; } @@ -112,9 +113,8 @@ public String getMapping() { @Override public String toString() { - return new StringBuilder().append("host:").append(host).append(";").append("accessKeyId:").append(accessKeyId).append(";").append("secretAccessKey:") - .append(secretAccessKey).append(";").append("custom:").append(hasCustomCredentials).append(";acl:").append(acl).append(";location:").append(this.defaultLocation) - .append(";").toString(); + return new StringBuilder().append("host:").append(getHost()).append(";").append("accessKeyId:").append(accessKeyId).append(";").append("secretAccessKey:") + .append(secretAccessKey).append(";acl:").append(acl).append(";location:").append(getDefaultLocation()).append(";").toString(); } public void setACL(Object acl) { @@ -247,6 +247,7 @@ private static S3Properties toS3(CFMLEngine eng, Struct sct) throws S3Exception private static S3Properties toS3(String accessKeyId, String awsSecretKey, String defaultLocation, String host, String bucket, String acl, TimeSpan cache) throws S3Exception { S3Properties s3 = new S3Properties(); + defaultLocation = S3Util.extractLocationFromHostIfNecessary(defaultLocation, host); if (!Util.isEmpty(accessKeyId)) s3.setAccessKeyId(accessKeyId); if (!Util.isEmpty(awsSecretKey)) s3.setSecretAccessKey(awsSecretKey); diff --git a/source/java/src/org/lucee/extension/resource/s3/S3ResourceProvider.java b/source/java/src/org/lucee/extension/resource/s3/S3ResourceProvider.java index 224af3b..014124d 100755 --- a/source/java/src/org/lucee/extension/resource/s3/S3ResourceProvider.java +++ b/source/java/src/org/lucee/extension/resource/s3/S3ResourceProvider.java @@ -154,6 +154,8 @@ public static String loadWithNewPattern(S3Properties properties, RefString stora if (Util.isEmpty(defaultLocation, true)) defaultLocation = S3Util.getSystemPropOrEnvVar("lucee.s3.defaultLocation", null); if (Util.isEmpty(defaultLocation, true)) defaultLocation = S3Util.getSystemPropOrEnvVar("lucee.s3.region", null); + defaultLocation = S3Util.extractLocationFromHostIfNecessary(defaultLocation, host); + bucket = S3Util.getSystemPropOrEnvVar("lucee.s3.bucket", null); if (Util.isEmpty(bucket, true)) bucket = S3Util.getSystemPropOrEnvVar("lucee.s3.bucketname", null); @@ -182,6 +184,9 @@ public static String loadWithNewPattern(S3Properties properties, RefString stora } if (!Util.isEmpty(prop.getDefaultLocation())) defaultLocation = prop.getDefaultLocation(); if (prop.getACL() != null) defaultACL = prop.getACL(); + + defaultLocation = S3Util.extractLocationFromHostIfNecessary(defaultLocation, host); + } } if (defaultACL != null) properties.setACL(defaultACL); @@ -228,6 +233,7 @@ public static String loadWithNewPattern(S3Properties properties, RefString stora secretAccessKey = prop.getSecretAccessKey(); defaultLocation = prop.getDefaultLocation(); defaultACL = prop.getACL(); + defaultLocation = S3Util.extractLocationFromHostIfNecessary(defaultLocation, host); } } diff --git a/source/java/src/org/lucee/extension/resource/s3/S3Util.java b/source/java/src/org/lucee/extension/resource/s3/S3Util.java index 47c7514..fc9aa0d 100644 --- a/source/java/src/org/lucee/extension/resource/s3/S3Util.java +++ b/source/java/src/org/lucee/extension/resource/s3/S3Util.java @@ -19,4 +19,19 @@ public static String getSystemPropOrEnvVar(String name, String defaultValue) { return defaultValue; } + + public static String extractLocationFromHostIfNecessary(String location, String host) { + if (!Util.isEmpty(location, true)) return location.trim(); + if (!Util.isEmpty(host, true) && host.startsWith("s3.")) { + for (String p: S3.PROVIDERS) { + host = host.trim(); + if (host.endsWith(p)) { + if (host.length() - p.length() < 3) continue; + return host.substring(3, host.length() - p.length()); + } + } + } + + return null; + } }