Skip to content

Commit b307450

Browse files
authored
Merge pull request rails#42504 from josegomezr/activestorage_s3_pass_client_opts
Passing extra parameters in `ActiveStorage::Blob#url` to S3 Client
2 parents 882750b + 302f708 commit b307450

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

activestorage/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
* Passing extra parameters in `ActiveStorage::Blob#url` to S3 Client
2+
3+
This allows calls of `ActiveStorage::Blob#url` to have more interaction with
4+
the S3 Presigner, enabling, amongst other options, custom S3 domain URL
5+
Generation.
6+
7+
```ruby
8+
blob = ActiveStorage::Blob.last
9+
10+
blob.url # => https://<bucket-name>.s3.<region>.amazonaws.com/<key>
11+
blob.url(virtual_host: true) # => # => https://<bucket-name>/<key>
12+
```
13+
14+
*josegomezr*
15+
116
* Allow setting a `Cache-Control` on files uploaded to GCS.
217

318
```yaml

activestorage/lib/active_storage/service/s3_service.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ def headers_for_direct_upload(key, content_type:, checksum:, filename: nil, disp
9696
end
9797

9898
private
99-
def private_url(key, expires_in:, filename:, disposition:, content_type:, **)
99+
def private_url(key, expires_in:, filename:, disposition:, content_type:, **client_opts)
100100
object_for(key).presigned_url :get, expires_in: expires_in.to_i,
101101
response_content_disposition: content_disposition_with(type: disposition, filename: filename),
102-
response_content_type: content_type
102+
response_content_type: content_type, **client_opts
103103
end
104104

105-
def public_url(key, **)
106-
object_for(key).public_url
105+
def public_url(key, **client_opts)
106+
object_for(key).public_url(**client_opts)
107107
end
108108

109109

activestorage/test/service/s3_public_service_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ class ActiveStorage::Service::S3PublicServiceTest < ActiveSupport::TestCase
2323
assert_equal "200", response.code
2424
end
2525

26+
test "public URL generation (virtual host enabled)" do
27+
url = @service.url(@key, filename: ActiveStorage::Filename.new("avatar.png"), virtual_host: true)
28+
29+
assert_match(/#{@service.bucket.name}\/#{@key}/, url)
30+
31+
response = Net::HTTP.get_response(URI(url))
32+
assert_equal "200", response.code
33+
end
34+
2635
test "direct upload" do
2736
key = SecureRandom.base58(24)
2837
data = "Something else entirely!"

0 commit comments

Comments
 (0)