Skip to content

Commit

Permalink
make cacheing region optional and possible to define via env var
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Feb 7, 2024
1 parent 3a27a23 commit 2bf7deb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions build.number
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
#Wed Jan 24 14:57:19 CET 2024
build.number=15
#Wed Feb 07 14:30:53 CET 2024
build.number=16
8 changes: 1 addition & 7 deletions source/java/src/org/lucee/extension/resource/s3/S3.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.lucee.extension.resource.s3.region.RegionFactory;
import org.lucee.extension.resource.s3.region.RegionFactory.Region;
import org.lucee.extension.resource.s3.util.XMLUtil;
import org.lucee.extension.resource.s3.util.print;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
Expand Down Expand Up @@ -161,7 +160,7 @@ public static S3 getInstance(S3Properties props, long cache, Config config) {
}
}
instances.put(keyS3, s3 = new S3(c, props.getAccessKeyId(), props.getSecretAccessKey(), props.getHost(), props.getDefaultLocation(), cache,
S3.DEFAULT_LIVE_TIMEOUT, true, config));
S3.DEFAULT_LIVE_TIMEOUT, props.getCacheRegion(), config));
}
}
}
Expand Down Expand Up @@ -227,7 +226,6 @@ private S3(S3Cache cache, String accessKeyId, String secretAccessKey, String hos
this.host = host;
this.cacheTimeout = cacheTimeout;
this.liveTimeout = liveTimeout;
print.ds();
if (!Util.isEmpty(defaultLocation, true)) {
try {
defaultRegion = toString(RegionFactory.getInstance(defaultLocation));
Expand Down Expand Up @@ -1038,22 +1036,18 @@ public List<S3Info> list(String bucketName, final String objectName, boolean rec
private List<S3Info> improveResult(ValidUntilMap<S3Info> objects, String bucketName, String objectName, boolean recursive, boolean listPseudoFolder, boolean onlyChildren,
boolean noCache) throws S3Exception {
bucketName = improveBucketName(bucketName);
print.e("--- improveResult ----");
print.e("- " + objects.size());
Iterator<S3Info> it = objects.values().iterator();
Map<String, S3Info> map = new LinkedHashMap<String, S3Info>();
S3Info info;
while (it.hasNext()) {
info = it.next();
add(map, info, objectName, recursive, listPseudoFolder, onlyChildren);
}
print.e("- " + map.size());
Iterator<S3Info> iit = map.values().iterator();
List<S3Info> list = new ArrayList<S3Info>();
while (iit.hasNext()) {
list.add(iit.next());
}
print.e("- " + list.size());
return list;
}

Expand Down
19 changes: 16 additions & 3 deletions source/java/src/org/lucee/extension/resource/s3/S3Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class S3Properties {
private String defaultLocation;
private long cache;
private String mapping;
private boolean cacheRegion = false;

public void setBucket(String bucket) {
if (!Util.isEmpty(bucket, true)) this.bucket = bucket;
Expand Down Expand Up @@ -116,11 +117,20 @@ public String getMapping() {
return mapping;
}

public boolean getCacheRegion() {
return cacheRegion;
}

public void setCacheRegion(boolean cacheRegion) {
this.cacheRegion = cacheRegion;
}

@Override
public String toString() {

return new StringBuilder().append("host:").append(getHost()).append(";").append("accessKeyId:").append(getAccessKeyId()).append(";").append("secretAccessKey:")
.append(getSecretAccessKey()).append(";acl:").append(getACL()).append(";location:").append(getDefaultLocation()).append(";").toString();
.append(getSecretAccessKey()).append(";acl:").append(getACL()).append(";location:").append(getDefaultLocation()).append(";cacheRegion:").append(getCacheRegion())
.append(";").toString();
}

public void setACL(Object acl) {
Expand Down Expand Up @@ -246,11 +256,13 @@ private static S3Properties toS3(CFMLEngine eng, Struct sct) throws S3Exception
if (Util.isEmpty(sk)) sk = eng.getCastUtil().toString(sct.get("secretKey", null), null);

return toS3(eng.getCastUtil().toString(sct.get("accessKeyId", null), null), sk, eng.getCastUtil().toString(sct.get("defaultLocation", null), null), host, bucket,
eng.getCastUtil().toString(sct.get("acl", null), null), eng.getCastUtil().toTimespan(sct.get("cache", null), null));
eng.getCastUtil().toString(sct.get("acl", null), null), eng.getCastUtil().toBoolean(sct.get("cacheregion", null), null),
eng.getCastUtil().toTimespan(sct.get("cache", null), null));

}

private static S3Properties toS3(String accessKeyId, String awsSecretKey, String defaultLocation, String host, String bucket, String acl, TimeSpan cache) throws S3Exception {
private static S3Properties toS3(String accessKeyId, String awsSecretKey, String defaultLocation, String host, String bucket, String acl, Boolean cacheRegion, TimeSpan cache)
throws S3Exception {

S3Properties s3 = new S3Properties();
defaultLocation = S3Util.extractLocationFromHostIfNecessary(defaultLocation, host);
Expand All @@ -261,6 +273,7 @@ private static S3Properties toS3(String accessKeyId, String awsSecretKey, String
if (!Util.isEmpty(host)) s3.setHost(host);
if (!Util.isEmpty(bucket)) s3.setBucket(bucket);
if (!Util.isEmpty(acl)) s3.setACL(AccessControlListUtil.toAccessControlList(acl));
if (cacheRegion != null) s3.setCacheRegion(cacheRegion.booleanValue());
if (cache != null) s3.setCache(cache.getMillis());

return s3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static String loadWithNewPattern(S3Properties properties, RefString stora
boolean hasCustomCredentials = false;
String accessKeyId, host, secretAccessKey, defaultLocation, bucket, mapping = null;
Object defaultACL;
Boolean cacheRegion = null;
Struct appData = null;

// env var / system prop
Expand All @@ -150,6 +151,15 @@ public static String loadWithNewPattern(S3Properties properties, RefString stora

defaultACL = S3Util.getSystemPropOrEnvVar("lucee.s3.acl", null);
if (defaultACL == null) defaultACL = S3Util.getSystemPropOrEnvVar("lucee.s3.accesscontrollist", null);

String tmp = S3Util.getSystemPropOrEnvVar("lucee.s3.cacheregion", null);
if (!Util.isEmpty(tmp, true)) {
try {
cacheRegion = Util.toBooleanValue(tmp.trim());
}
catch (Exception e) {
}
}
}

// Application Context Data
Expand Down Expand Up @@ -270,6 +280,7 @@ public static String loadWithNewPattern(S3Properties properties, RefString stora
properties.setCustomCredentials(hasCustomCredentials);
properties.setCustomHost(hasCustomHost);
properties.setMapping(mapping);
if (cacheRegion != null) properties.setCacheRegion(cacheRegion);
if (defaultACL != null) properties.setACL(defaultACL);
if (defaultLocation != null) properties.setDefaultLocation(defaultLocation);
if (!hasCustomHost && !Util.isEmpty(host, true)) properties.setHost(host);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ protected static S3Properties toS3Properties(PageContext pc, String accessKeyId,
if (Util.isEmpty(host, true)) host = S3Util.getSystemPropOrEnvVar("lucee.s3.server", null);
if (Util.isEmpty(host, true)) host = S3Util.getSystemPropOrEnvVar("lucee.s3.provider", null);

String strCacheRegion = S3Util.getSystemPropOrEnvVar("lucee.s3.cacheregion", null);
Boolean cacheRegion = (Util.isEmpty(strCacheRegion, true)) ? null : CFMLEngineFactory.getInstance().getCastUtil().toBoolean(strCacheRegion.trim(), null);

if (Util.isEmpty(secretAccessKey, true) || Util.isEmpty(accessKeyId, true)) throw CFMLEngineFactory.getInstance().getExceptionUtil().createApplicationException(
"missing S3 credentials",
"you can define the credentials as argument for the function [accessKeyId, secretAccessKey], in the application.cfc [this.s3.accessKeyId, this.s3.secretAccessKey] or in the system properties/environment variables [lucee.s3.secretaccesskey,lucee.s3.accesskeyid]");
Expand All @@ -107,6 +110,7 @@ protected static S3Properties toS3Properties(PageContext pc, String accessKeyId,
props.setCustomHost(true);
}
else props.setCustomHost(false);
if (cacheRegion != null) props.setCacheRegion(cacheRegion.booleanValue());

return props;
}
Expand Down

0 comments on commit 2bf7deb

Please sign in to comment.