Skip to content

Commit

Permalink
Make sure we have no separate instances when loading via virtual file…
Browse files Browse the repository at this point in the history
…sytem and S3 functions, because this can cause a caching delay.
  • Loading branch information
michaeloffner committed Oct 4, 2023
1 parent 41fd843 commit 9ee1da6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions source/java/src/org/lucee/extension/resource/s3/S3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

}
}
Expand Down
15 changes: 15 additions & 0 deletions source/java/src/org/lucee/extension/resource/s3/S3Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 9ee1da6

Please sign in to comment.