This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new befe8327212a CAMEL-23123: Fix S3Presigner ignoring forcePathStyle 
configuration
befe8327212a is described below

commit befe8327212a79cd003b201b28082a179ca149c9
Author: Croway <[email protected]>
AuthorDate: Tue Mar 3 10:46:16 2026 +0100

    CAMEL-23123: Fix S3Presigner ignoring forcePathStyle configuration
    
    Co-Authored-By: Bruno Meseguer <[email protected]>
---
 .../camel/component/aws2/s3/AWS2S3Producer.java    | 68 +++++++---------
 .../integration/S3PresignerForcePathStyleIT.java   | 92 ++++++++++++++++++++++
 2 files changed, 120 insertions(+), 40 deletions(-)

diff --git 
a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Producer.java
 
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Producer.java
index 01eb91ac8d26..7dfc50df801b 100644
--- 
a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Producer.java
+++ 
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Producer.java
@@ -49,6 +49,7 @@ import software.amazon.awssdk.core.sync.RequestBody;
 import software.amazon.awssdk.core.sync.ResponseTransformer;
 import software.amazon.awssdk.regions.Region;
 import software.amazon.awssdk.services.s3.S3Client;
+import software.amazon.awssdk.services.s3.S3Configuration;
 import software.amazon.awssdk.services.s3.model.*;
 import software.amazon.awssdk.services.s3.presigner.S3Presigner;
 import 
software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
@@ -713,26 +714,7 @@ public class AWS2S3Producer extends DefaultProducer {
         } else {
             milliSeconds += 1000 * 60 * 60;
         }
-        S3Presigner presigner;
-
-        if 
(ObjectHelper.isNotEmpty(getConfiguration().getAmazonS3Presigner())) {
-            presigner = getConfiguration().getAmazonS3Presigner();
-        } else {
-            S3Presigner.Builder builder = S3Presigner.builder();
-            builder.credentialsProvider(
-                    getConfiguration().isUseDefaultCredentialsProvider()
-                            ? DefaultCredentialsProvider.create() : 
StaticCredentialsProvider.create(
-                                    
AwsBasicCredentials.create(getConfiguration().getAccessKey(),
-                                            
getConfiguration().getSecretKey())))
-                    .region(Region.of(getConfiguration().getRegion()));
-
-            String uriEndpointOverride = 
getConfiguration().getUriEndpointOverride();
-            if (ObjectHelper.isNotEmpty(uriEndpointOverride)) {
-                builder.endpointOverride(URI.create(uriEndpointOverride));
-            }
-
-            presigner = builder.build();
-        }
+        S3Presigner presigner = getOrCreatePresigner();
 
         GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                 .bucket(bucketName)
@@ -1032,26 +1014,7 @@ public class AWS2S3Producer extends DefaultProducer {
         } else {
             milliSeconds += 1000 * 60 * 60;
         }
-        S3Presigner presigner;
-
-        if 
(ObjectHelper.isNotEmpty(getConfiguration().getAmazonS3Presigner())) {
-            presigner = getConfiguration().getAmazonS3Presigner();
-        } else {
-            S3Presigner.Builder builder = S3Presigner.builder();
-            builder.credentialsProvider(
-                    getConfiguration().isUseDefaultCredentialsProvider()
-                            ? DefaultCredentialsProvider.create() : 
StaticCredentialsProvider.create(
-                                    
AwsBasicCredentials.create(getConfiguration().getAccessKey(),
-                                            
getConfiguration().getSecretKey())))
-                    .region(Region.of(getConfiguration().getRegion()));
-
-            String uriEndpointOverride = 
getConfiguration().getUriEndpointOverride();
-            if (ObjectHelper.isNotEmpty(uriEndpointOverride)) {
-                builder.endpointOverride(URI.create(uriEndpointOverride));
-            }
-
-            presigner = builder.build();
-        }
+        S3Presigner presigner = getOrCreatePresigner();
 
         PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                 .bucket(bucketName)
@@ -1088,6 +1051,31 @@ public class AWS2S3Producer extends DefaultProducer {
         }
     }
 
+    private S3Presigner getOrCreatePresigner() {
+        if 
(ObjectHelper.isNotEmpty(getConfiguration().getAmazonS3Presigner())) {
+            return getConfiguration().getAmazonS3Presigner();
+        }
+
+        S3Presigner.Builder builder = S3Presigner.builder();
+        builder.credentialsProvider(
+                getConfiguration().isUseDefaultCredentialsProvider()
+                        ? DefaultCredentialsProvider.create() : 
StaticCredentialsProvider.create(
+                                
AwsBasicCredentials.create(getConfiguration().getAccessKey(),
+                                        getConfiguration().getSecretKey())))
+                .region(Region.of(getConfiguration().getRegion()));
+
+        if (getConfiguration().isForcePathStyle()) {
+            
builder.serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).build());
+        }
+
+        String uriEndpointOverride = 
getConfiguration().getUriEndpointOverride();
+        if (ObjectHelper.isNotEmpty(uriEndpointOverride)) {
+            builder.endpointOverride(URI.create(uriEndpointOverride));
+        }
+
+        return builder.build();
+    }
+
     private void createBucket(S3Client s3Client, Exchange exchange) throws 
InvalidPayloadException {
         final String bucketName = AWS2S3Utils.determineBucketName(exchange, 
getConfiguration());
 
diff --git 
a/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/integration/S3PresignerForcePathStyleIT.java
 
b/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/integration/S3PresignerForcePathStyleIT.java
new file mode 100644
index 000000000000..bfad9249cbe4
--- /dev/null
+++ 
b/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/integration/S3PresignerForcePathStyleIT.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws2.s3.integration;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.aws2.s3.AWS2S3Constants;
+import org.apache.camel.component.aws2.s3.AWS2S3Operations;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class S3PresignerForcePathStyleIT extends Aws2S3Base {
+
+    @EndpointInject
+    private ProducerTemplate template;
+
+    @Test
+    public void downloadLinkShouldUsePathStyleWhenForcePathStyleEnabled() {
+        Exchange ex = template.request("direct:createDownloadLinkPathStyle", 
new Processor() {
+            public void process(Exchange exchange) {
+                exchange.getIn().setHeader(AWS2S3Constants.KEY, 
"CamelUnitTest2");
+                exchange.getIn().setHeader(AWS2S3Constants.BUCKET_NAME, 
name.get());
+                exchange.getIn().setHeader(AWS2S3Constants.S3_OPERATION, 
AWS2S3Operations.createDownloadLink);
+            }
+        });
+
+        String downloadLink = ex.getMessage().getBody(String.class);
+        assertNotNull(downloadLink);
+        // With forcePathStyle=true, the URL should use path-style: 
http://localhost:8080/<bucket>/...
+        // instead of virtual-hosted-style: http://<bucket>.localhost:8080/...
+        assertTrue(downloadLink.startsWith("http://localhost:8080/"; + 
name.get()),
+                "Expected path-style URL starting with http://localhost:8080/"; 
+ name.get() + " but got: " + downloadLink);
+    }
+
+    @Test
+    public void downloadLinkShouldUseVirtualHostedStyleByDefault() {
+        Exchange ex = 
template.request("direct:createDownloadLinkVirtualHosted", new Processor() {
+            public void process(Exchange exchange) {
+                exchange.getIn().setHeader(AWS2S3Constants.KEY, 
"CamelUnitTest2");
+                exchange.getIn().setHeader(AWS2S3Constants.BUCKET_NAME, 
name.get());
+                exchange.getIn().setHeader(AWS2S3Constants.S3_OPERATION, 
AWS2S3Operations.createDownloadLink);
+            }
+        });
+
+        String downloadLink = ex.getMessage().getBody(String.class);
+        assertNotNull(downloadLink);
+        // Without forcePathStyle, the URL should use virtual-hosted-style: 
http://<bucket>.localhost:8080/...
+        assertTrue(downloadLink.startsWith("http://"; + name.get() + 
".localhost:8080"),
+                "Expected virtual-hosted-style URL starting with http://"; + 
name.get()
+                                                                               
         + ".localhost:8080 but got: "
+                                                                               
         + downloadLink);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                String awsEndpoint = "aws2-s3://" + name.get() + 
"?autoCreateBucket=false";
+
+                from("direct:createDownloadLinkPathStyle")
+                        .to(awsEndpoint
+                            + "&accessKey=xxx&secretKey=yyy&region=eu-west-1"
+                            + 
"&forcePathStyle=true&uriEndpointOverride=http://localhost:8080";);
+
+                from("direct:createDownloadLinkVirtualHosted")
+                        .to(awsEndpoint
+                            + "&accessKey=xxx&secretKey=yyy&region=eu-west-1"
+                            + "&uriEndpointOverride=http://localhost:8080";);
+            }
+        };
+    }
+}

Reply via email to