Skip to content

Commit 80a5692

Browse files
authored
Merge pull request #1162 from tuist/fix/bucket-as-host-double-domain
Fix bucket_as_host with virtual_host causing double bucket in URLs
2 parents c691668 + 76b39d8 commit 80a5692

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/ex_aws/operation/s3.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ defmodule ExAws.Operation.S3 do
6262
raise "#{__MODULE__}.perform/2 cannot perform operation on `nil` bucket"
6363
end
6464

65+
def add_bucket_to_path(operation, %{virtual_host: true, bucket_as_host: true} = config) do
66+
# When bucket_as_host is true, use the bucket name as the full hostname
67+
{put_in(operation.path, ensure_absolute(operation.path)),
68+
Map.put(config, :host, operation.bucket)}
69+
end
70+
6571
def add_bucket_to_path(operation, %{virtual_host: true, host: base_host} = config) do
6672
vhost_domain = "#{operation.bucket}.#{base_host}"
6773

test/ex_aws/operation/s3_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,24 @@ defmodule ExAws.Operation.S3Test do
8080
{processed_operation, _processed_config} = S3.add_bucket_to_path(operation, config)
8181
assert processed_operation.path == "/folder/"
8282
end
83+
84+
test "S3 uses bucket as host when both virtual_host and bucket_as_host are true" do
85+
config = ExAws.Config.new(:s3) |> Map.put(:virtual_host, true) |> Map.put(:bucket_as_host, true)
86+
operation = s3_operation("my-custom-domain.com")
87+
88+
{processed_operation, processed_config} = S3.add_bucket_to_path(operation, config)
89+
90+
assert(processed_config.host == "my-custom-domain.com")
91+
assert(processed_operation.path == "/folder")
92+
end
93+
94+
test "S3 uses standard virtual host when virtual_host is true but bucket_as_host is false" do
95+
config = ExAws.Config.new(:s3) |> Map.put(:virtual_host, true) |> Map.put(:bucket_as_host, false)
96+
operation = s3_operation()
97+
98+
{processed_operation, processed_config} = S3.add_bucket_to_path(operation, config)
99+
100+
assert(processed_config.host == "#{operation.bucket}.#{ExAws.Config.new(:s3).host}")
101+
assert(processed_operation.path == "/folder")
102+
end
83103
end

0 commit comments

Comments
 (0)