From dabd13508f4be9f7b48906d03b6716f2037c55de Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Thu, 28 Sep 2023 13:11:46 +0200 Subject: [PATCH] LDEV-4707 add addional arguments to the function --- build.number | 4 ++-- source/fld/function.fld | 7 ++++++ .../org/lucee/extension/resource/s3/S3.java | 24 +++++++++++++++++-- .../s3/function/S3GeneratePresignedURL.java | 14 ++++++----- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/build.number b/build.number index aff5811..f30b538 100644 --- a/build.number +++ b/build.number @@ -1,3 +1,3 @@ #Build Number for ANT. Do not edit! -#Thu Sep 28 09:34:15 CEST 2023 -build.number=15 +#Thu Sep 28 13:07:44 CEST 2023 +build.number=16 diff --git a/source/fld/function.fld b/source/fld/function.fld index 3a9a99f..bc0290b 100755 --- a/source/fld/function.fld +++ b/source/fld/function.fld @@ -911,6 +911,13 @@ boolean No A flag to specify if the object has zero-byte content. + + + responseHeaders + customResponseHeaders + struct + No + Struct of custom response headers for custom metadata prefixed with "x-amz-meta-" (prefix is optional, function will add it if missed). accessKeyId 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 d29895b..358a7c1 100755 --- a/source/java/src/org/lucee/extension/resource/s3/S3.java +++ b/source/java/src/org/lucee/extension/resource/s3/S3.java @@ -75,6 +75,7 @@ import lucee.runtime.type.Struct; import lucee.runtime.util.Cast; import lucee.runtime.util.Creation; +import lucee.runtime.util.Strings; public class S3 { private static final short CHECK_EXISTS = 1; @@ -409,7 +410,8 @@ public S3ObjectSummary getInfo(String bucketName, String objectName) throws S3Ex * @param contentEncoding Specifies content encodings applied to the object, like gzip. * @param versionId The version ID of the object if versioning is enabled. * @param zeroByteContent A flag to specify if the object has zero-byte content. - * @param responseHeaders Struct of response headers. + * @param customResponseHeaders Struct of custom response headers for custom metadata prefixed with + * "x-amz-meta-" (prefix is optional). * * @return The generated pre-signed URL. * @@ -417,7 +419,7 @@ public S3ObjectSummary getInfo(String bucketName, String objectName) throws S3Ex * parameters. */ public URL generatePresignedURL(String bucketName, String objectName, Date expireDate, String httpMethod, String sseAlgorithm, String sseCustomerKey, String checksum, - String contentType, String contentDisposition, String contentEncoding, String versionId, Boolean zeroByteContent) throws S3Exception { + String contentType, String contentDisposition, String contentEncoding, String versionId, Boolean zeroByteContent, Struct customResponseHeaders) throws S3Exception { bucketName = improveBucketName(bucketName); objectName = improveObjectName(objectName); @@ -515,8 +517,26 @@ public URL generatePresignedURL(String bucketName, String objectName, Date expir generatePresignedUrlRequest.withExpiration(expireDate); } + if (customResponseHeaders != null && !customResponseHeaders.isEmpty()) { + Iterator> it = customResponseHeaders.entryIterator(); + Entry e; + String name; + CFMLEngine eng = CFMLEngineFactory.getInstance(); + Strings util = CFMLEngineFactory.getInstance().getStringUtil(); + Cast caster = eng.getCastUtil(); + while (it.hasNext()) { + e = it.next(); + name = e.getKey().getString(); + if (!util.startsWithIgnoreCase(name, "x-amz-meta-")) name = "x-amz-meta-" + name; + generatePresignedUrlRequest.addRequestParameter(name, caster.toString(e.getValue())); + } + } + return client.generatePresignedUrl(generatePresignedUrlRequest); } + catch (PageException pe) { + throw toS3Exception(pe); + } catch (AmazonServiceException ase) { throw toS3Exception(ase); } diff --git a/source/java/src/org/lucee/extension/resource/s3/function/S3GeneratePresignedURL.java b/source/java/src/org/lucee/extension/resource/s3/function/S3GeneratePresignedURL.java index fd299e8..bc0df47 100644 --- a/source/java/src/org/lucee/extension/resource/s3/function/S3GeneratePresignedURL.java +++ b/source/java/src/org/lucee/extension/resource/s3/function/S3GeneratePresignedURL.java @@ -11,6 +11,7 @@ import lucee.loader.util.Util; import lucee.runtime.PageContext; import lucee.runtime.exp.PageException; +import lucee.runtime.type.Struct; import lucee.runtime.type.dt.DateTime; import lucee.runtime.util.Cast; @@ -24,7 +25,7 @@ public Object invoke(PageContext pc, Object[] args) throws PageException { CFMLEngine eng = CFMLEngineFactory.getInstance(); Cast cast = eng.getCastUtil(); - if (args.length < 1 || args.length < 16) throw eng.getExceptionUtil().createFunctionException(pc, "S3GeneratePresignedURL", 1, 16, args.length); + if (args.length < 1 || args.length < 17) throw eng.getExceptionUtil().createFunctionException(pc, "S3GeneratePresignedURL", 1, 17, args.length); String tmp; // required @@ -42,10 +43,11 @@ public Object invoke(PageContext pc, Object[] args) throws PageException { String contentEncoding = args.length > 9 && args[9] != null ? cast.toString(args[9]) : null; String versionId = args.length > 10 && args[10] != null ? cast.toString(args[10]) : null; Boolean zeroByteContent = args.length > 11 && !isEmpty(args[11]) ? cast.toBoolean(args[11]) : null; - String accessKeyId = args.length > 12 && args[12] != null ? cast.toString(args[12]) : null; - String secretAccessKey = args.length > 13 && args[13] != null ? cast.toString(args[13]) : null; - String host = args.length > 14 && args[14] != null ? cast.toString(args[14]) : null; - double timeout = args.length > 15 && !isEmpty(args[15]) ? cast.toDoubleValue(args[15]) : null; + Struct customResponseHeaders = args.length > 12 && !isEmpty(args[12]) ? cast.toStruct(args[12]) : null; + String accessKeyId = args.length > 13 && args[13] != null ? cast.toString(args[13]) : null; + String secretAccessKey = args.length > 14 && args[14] != null ? cast.toString(args[14]) : null; + String host = args.length > 15 && args[15] != null ? cast.toString(args[15]) : null; + double timeout = args.length > 16 && !isEmpty(args[16]) ? cast.toDoubleValue(args[16]) : null; // for backward compatibility, when host was not existing if (eng.getDecisionUtil().isNumber(host)) { @@ -67,7 +69,7 @@ public Object invoke(PageContext pc, Object[] args) throws PageException { } return s3.generatePresignedURL(bucketNameOrPath, objectName, expireDate, httpMethod, sseAlgorithm, sseCustomerKey, checksum, contentType, contentDisposition, - contentEncoding, versionId, zeroByteContent).toExternalForm(); + contentEncoding, versionId, zeroByteContent, customResponseHeaders).toExternalForm(); } catch (Exception e) {