Skip to content

Commit

Permalink
add function S3GetVersionInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Jun 17, 2024
1 parent 2bf7deb commit 39b4160
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 4 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 Feb 07 14:30:53 CET 2024
build.number=16
#Mon Jun 17 18:54:49 CEST 2024
build.number=17
55 changes: 55 additions & 0 deletions source/fld/function.fld
Original file line number Diff line number Diff line change
Expand Up @@ -1522,4 +1522,59 @@
<type>struct</type>
</return>
</function>


<!-- S3GetVersionInfo -->
<!-- S3GetVersionInfo -->
<function>
<name>S3GetVersionInfo</name>
<class bundle-name="{bundle-name}" bundle-version="{bundle-version}">org.lucee.extension.resource.s3.function.S3GetVersionInfo</class>
<description>Returns version information as a query for a specific object or bucket.</description>
<argument>
<name>bucketName</name>
<alias>bucket</alias>
<type>string</type>
<required>Yes</required>
<description>Name of the bucket to get info for.</description>
</argument>
<argument>
<name>objectName</name>
<alias>object,path</alias>
<type>string</type>
<required>No</required>
<description>Name of the object (path) within the bucket to get info for. If not defined, info of the bucket is returned.</description>
</argument>
<argument>
<name>accessKeyId</name>
<alias>accessKey,awsAccessKeyId,awsAccessKey</alias>
<type>string</type>
<required>No</required>
<description>S3 accessKeyId. If not defined, checks the system property/environment variable for [lucee.s3.accesskeyid].</description>
</argument>
<argument>
<name>secretAccessKey</name>
<alias>secretKey,awsSecretKey,awsSecretAccessKey</alias>
<type>string</type>
<required>No</required>
<description>S3 secretAccessKey. If not defined, checks the system property/environment variable for [lucee.s3.secretaccesskey].</description>
</argument>
<argument>
<name>host</name>
<alias>provider,server</alias>
<type>string</type>
<required>No</required>
<description>The provider to connect to. If not set, Amazon AWS is used.</description>
</argument>
<argument>
<name>timeout</name>
<type>number</type>
<required>No</required>
<default>10000</default>
<description>Timeout for this execution (in milliseconds).</description>
</argument>
<return>
<type>query</type>
</return>
</function>

</func-lib>
46 changes: 45 additions & 1 deletion source/java/src/org/lucee/extension/resource/s3/S3.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ public S3Object getData(String bucketName, String objectName) throws S3Exception
AmazonS3Client client = getAmazonS3(bucketName, null);
try {
if (log != null) log.debug("S3", "get data from [" + bucketName + "/" + objectName + "]");
if (log != null) log.debug("S3", "get [" + bucketName + "/" + objectName + "]");
return client.getObject(bucketName, objectName);
}
catch (AmazonServiceException se) {
Expand All @@ -460,6 +459,51 @@ public S3Object getData(String bucketName, String objectName) throws S3Exception
}
}

public Query listVersions(String bucketName, String objectName) throws S3Exception, PageException {
bucketName = improveBucketName(bucketName);
objectName = improveObjectName(objectName);
CFMLEngine eng = CFMLEngineFactory.getInstance();
Creation creator = eng.getCreationUtil();
Cast caster = eng.getCastUtil();
Query data = creator.createQuery(new String[] { "id", "lasModified", "size", "etag", "stotageClass", "owner", "isLatest" }, 0, "versions");
int row;
AmazonS3Client client = getAmazonS3(bucketName, null);
try {

ListVersionsRequest request = new ListVersionsRequest().withBucketName(bucketName);
if (objectName != null) request.withPrefix(objectName);

if (log != null) log.debug("S3", "read versions from [" + bucketName + "/" + objectName + "]");

VersionListing versionListing;
do {
versionListing = client.listVersions(request);
if (versionListing != null) {
for (S3VersionSummary versionSummary: versionListing.getVersionSummaries()) {
row = data.addRow();
data.setAt("id", row, versionSummary.getVersionId());
data.setAt("lasModified", row, caster.toDatetime(versionSummary.getLastModified(), null));
data.setAt("isLatest", row, versionSummary.isLatest());
data.setAt("size", row, versionSummary.getSize());
data.setAt("etag", row, versionSummary.getETag());
data.setAt("stotageClass", row, versionSummary.getStorageClass());
data.setAt("owner", row, versionSummary.getOwner() == null ? "" : versionSummary.getOwner().getDisplayName());
}
}
request.setKeyMarker(versionListing.getNextKeyMarker());
request.setVersionIdMarker(versionListing.getNextVersionIdMarker());
}
while (versionListing.isTruncated());
return data;
}
catch (AmazonServiceException se) {
throw toS3Exception(se);
}
finally {
client.release();
}
}

public InputStream getInputStream(String bucketName, String objectName) throws S3Exception {
bucketName = improveBucketName(bucketName);
objectName = improveObjectName(objectName, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ public static String loadWithNewPattern(S3Properties properties, RefString stora
{
S3Properties prop = null;
if (pc != null) {
appData = S3Properties.getApplicationData(pc);
appData = null;
try {
appData = S3Properties.getApplicationData(pc);
}
catch (Exception e) {
}
prop = appData != null ? S3Properties.load(pc, appData, null) : null;// pc.getApplicationContext().getS3();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.lucee.extension.resource.s3.function;

import org.lucee.extension.resource.s3.S3;

import lucee.loader.engine.CFMLEngine;
import lucee.loader.engine.CFMLEngineFactory;
import lucee.runtime.PageContext;
import lucee.runtime.exp.PageException;
import lucee.runtime.type.Query;
import lucee.runtime.util.Cast;

public class S3GetVersionInfo extends S3Function {

private static final long serialVersionUID = 811465114006696746L;

public static Query call(PageContext pc, String bucketName, String objectName, String accessKeyId, String secretAccessKey, String host, double timeout) throws PageException {
CFMLEngine eng = CFMLEngineFactory.getInstance();
// for backward compatibility, when host was not existing
if (eng.getDecisionUtil().isNumber(host)) {
timeout = eng.getCastUtil().toDoubleValue(host);
host = null;
}
try {
// create S3 Instance
S3 s3 = S3.getInstance(toS3Properties(pc, accessKeyId, secretAccessKey, host), toTimeout(timeout), pc.getConfig());
return s3.listVersions(bucketName, objectName);

}
catch (Exception e) {
throw eng.getCastUtil().toPageException(e);
}
}

@Override
public Object invoke(PageContext pc, Object[] args) throws PageException {
CFMLEngine engine = CFMLEngineFactory.getInstance();
Cast cast = engine.getCastUtil();

if (args.length == 6)
return call(pc, cast.toString(args[0]), cast.toString(args[1]), cast.toString(args[2]), cast.toString(args[3]), cast.toString(args[4]), cast.toDoubleValue(args[5]));
if (args.length == 5) return call(pc, cast.toString(args[0]), cast.toString(args[1]), cast.toString(args[2]), cast.toString(args[3]), cast.toString(args[4]), 0);
if (args.length == 4) return call(pc, cast.toString(args[0]), cast.toString(args[1]), cast.toString(args[2]), cast.toString(args[3]), null, 0);
if (args.length == 3) return call(pc, cast.toString(args[0]), cast.toString(args[1]), cast.toString(args[2]), null, null, 0);
if (args.length == 2) return call(pc, cast.toString(args[0]), cast.toString(args[1]), null, null, null, 0);
if (args.length == 1) return call(pc, cast.toString(args[0]), null, null, null, null, 0);

throw engine.getExceptionUtil().createFunctionException(pc, "S3GetVersionInfo", 1, 6, args.length);
}
}

0 comments on commit 39b4160

Please sign in to comment.