Skip to content

Commit 1bd092c

Browse files
authored
Merge pull request rails#42509 from maleblond/add-cache-control-support-active-support-gcs
Support default `cache_control` in GCS Active Storage
2 parents c9a0f1a + a22710e commit 1bd092c

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

activestorage/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* Allow setting a `Cache-Control` on files uploaded to GCS.
2+
3+
```yaml
4+
gcs:
5+
service: GCS
6+
...
7+
cache_control: "public, max-age=3600"
8+
```
9+
*maleblond*
110
211
* The parameters sent to `ffmpeg` for generating a video preview image are now
312
configurable under `config.active_storage.video_preview_arguments`.

activestorage/lib/active_storage/service/gcs_service.rb

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename
1919
# binary and attachment when the file's content type requires it. The only way to force them is to
2020
# store them as object's metadata.
2121
content_disposition = content_disposition_with(type: disposition, filename: filename) if disposition && filename
22-
bucket.create_file(io, key, md5: checksum, content_type: content_type, content_disposition: content_disposition)
22+
bucket.create_file(io, key, md5: checksum, cache_control: @config[:cache_control], content_type: content_type, content_disposition: content_disposition)
2323
rescue Google::Cloud::InvalidArgumentError
2424
raise ActiveStorage::IntegrityError
2525
end
@@ -84,7 +84,24 @@ def exist?(key)
8484

8585
def url_for_direct_upload(key, expires_in:, checksum:, **)
8686
instrument :url, key: key do |payload|
87-
generated_url = bucket.signed_url key, method: "PUT", expires: expires_in, content_md5: checksum
87+
headers = {}
88+
version = :v2
89+
90+
if @config[:cache_control].present?
91+
headers["Cache-Control"] = @config[:cache_control]
92+
# v2 signing doesn't support non `x-goog-` headers. Only switch to v4 signing
93+
# if necessary for back-compat; v4 limits the expiration of the URL to 7 days
94+
# whereas v2 has no limit
95+
version = :v4
96+
end
97+
98+
generated_url = bucket.signed_url(key,
99+
content_md5: checksum,
100+
expires: expires_in,
101+
headers: headers,
102+
method: "PUT",
103+
version: version
104+
)
88105

89106
payload[:url] = generated_url
90107

@@ -95,7 +112,13 @@ def url_for_direct_upload(key, expires_in:, checksum:, **)
95112
def headers_for_direct_upload(key, checksum:, filename: nil, disposition: nil, **)
96113
content_disposition = content_disposition_with(type: disposition, filename: filename) if filename
97114

98-
{ "Content-MD5" => checksum, "Content-Disposition" => content_disposition }
115+
headers = { "Content-MD5" => checksum, "Content-Disposition" => content_disposition }
116+
117+
if @config[:cache_control].present?
118+
headers["Cache-Control"] = @config[:cache_control]
119+
end
120+
121+
headers
99122
end
100123

101124
private
@@ -137,7 +160,7 @@ def bucket
137160
end
138161

139162
def client
140-
@client ||= Google::Cloud::Storage.new(**config.except(:bucket))
163+
@client ||= Google::Cloud::Storage.new(**config.except(:bucket, :cache_control))
141164
end
142165
end
143166
end

activestorage/test/service/gcs_service_test.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase
5757
@service.delete key
5858
end
5959

60+
test "direct upload with cache control" do
61+
config_with_cache_control = { gcs: service_config[:gcs].merge({ cache_control: "public, max-age=1800" }) }
62+
service = ActiveStorage::Service.configure(:gcs, config_with_cache_control)
63+
64+
key = SecureRandom.base58(24)
65+
data = "Some text"
66+
checksum = Digest::MD5.base64digest(data)
67+
url = service.url_for_direct_upload(key, expires_in: 5.minutes, content_type: "text/plain", content_length: data.size, checksum: checksum)
68+
69+
uri = URI.parse url
70+
request = Net::HTTP::Put.new uri.request_uri
71+
request.body = data
72+
headers = service.headers_for_direct_upload(key, checksum: checksum, filename: ActiveStorage::Filename.new("test.txt"), disposition: :attachment)
73+
assert_equal(headers["Cache-Control"], "public, max-age=1800")
74+
75+
headers.each do |k, v|
76+
request.add_field k, v
77+
end
78+
request.add_field "Content-Type", ""
79+
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
80+
http.request request
81+
end
82+
83+
url = service.url(key, expires_in: 2.minutes, disposition: :inline, content_type: "text/html", filename: ActiveStorage::Filename.new("test.html"))
84+
response = Net::HTTP.get_response(URI(url))
85+
assert_equal("public, max-age=1800", response["Cache-Control"])
86+
ensure
87+
service.delete(key)
88+
end
89+
6090
test "upload with content_type and content_disposition" do
6191
key = SecureRandom.base58(24)
6292
data = "Something else entirely!"
@@ -85,6 +115,23 @@ class ActiveStorage::Service::GCSServiceTest < ActiveSupport::TestCase
85115
@service.delete key
86116
end
87117

118+
test "upload with cache_control" do
119+
key = SecureRandom.base58(24)
120+
data = "Something else entirely!"
121+
122+
config_with_cache_control = { gcs: service_config[:gcs].merge({ cache_control: "public, max-age=1800" }) }
123+
service = ActiveStorage::Service.configure(:gcs, config_with_cache_control)
124+
125+
service.upload(key, StringIO.new(data), checksum: Digest::MD5.base64digest(data), content_type: "text/plain")
126+
127+
url = service.url(key, expires_in: 2.minutes, disposition: :inline, content_type: "text/html", filename: ActiveStorage::Filename.new("test.html"))
128+
129+
response = Net::HTTP.get_response(URI(url))
130+
assert_equal "public, max-age=1800", response["Cache-Control"]
131+
ensure
132+
service.delete key
133+
end
134+
88135
test "update metadata" do
89136
key = SecureRandom.base58(24)
90137
data = "Something else entirely!"

guides/source/active_storage_overview.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ google:
235235
bucket: ""
236236
```
237237

238+
Optionally provide a Cache-Control metadata to set on uploaded assets:
239+
240+
```yaml
241+
google:
242+
service: GCS
243+
...
244+
cache_control: "public, max-age=3600"
245+
```
246+
238247
Add the [`google-cloud-storage`](https://github.com/GoogleCloudPlatform/google-cloud-ruby/tree/master/google-cloud-storage) gem to your `Gemfile`:
239248

240249
```ruby
@@ -822,6 +831,7 @@ Take care to allow:
822831
* `Content-Disposition` (except for Azure Storage)
823832
* `x-ms-blob-content-disposition` (for Azure Storage only)
824833
* `x-ms-blob-type` (for Azure Storage only)
834+
* `Cache-Control` (for GCS, only if `cache_control` is set)
825835

826836
No CORS configuration is required for the Disk service since it shares your app’s origin.
827837

0 commit comments

Comments
 (0)