Module: S3Sign::Helper
- Defined in:
- lib/s3_sign/helper.rb
Overview
The S3Sign::Helper module provides two methods for generating S3 signed URLs with different caching strategies, designed to be included in Rails controllers.
Class Method Summary collapse
Instance Method Summary collapse
-
#s3_signed_url_for_key(key, options = {}) ⇒ Object
A pure “time from now” signed s3 asset url.
-
#stable_s3_signed_url(url, options = {}) ⇒ Object
Uses a far-future date so that the expires and signature on the url doesn’t change for every request.
Class Method Details
.included(base) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/s3_sign/helper.rb', line 9 def self.included(base) if base.respond_to?(:helper_method) base.helper_method :stable_s3_signed_url base.helper_method :s3_signed_url_for_key end return unless base.respond_to?(:hide_action) base.hide_action :stable_s3_signed_url base.hide_action :s3_signed_url_for_key end |
Instance Method Details
#s3_signed_url_for_key(key, options = {}) ⇒ Object
A pure “time from now” signed s3 asset url. Good for non-visual elements like attachments, and not thumbnails, that don’t have the same caching concerns
23 24 25 26 |
# File 'lib/s3_sign/helper.rb', line 23 def s3_signed_url_for_key(key, = {}) [:expires] ||= 86_400 S3Sign.url key, end |
#stable_s3_signed_url(url, options = {}) ⇒ Object
Uses a far-future date so that the expires and signature on the url doesn’t change for every request. This lets us still protect assets but the browser can cache them because the url isn’t constantly changing.
Use for assets we want browser cached that don’t need higher level protection, such as thumbnails. Other more central assets like downloads for a course or videos should have short expiration because they generally don’t benefit as much from caching and need better time-based protection.
If a reference_time is given, that will use the time given but in the far future stable year. This is useful to cache-bust the url from an updated_at timestamp where some browsers/proxies may try to use the cached asset even though it was updated.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/s3_sign/helper.rb', line 43 def stable_s3_signed_url(url, = {}) @stable_s3_expire_at ||= Time.parse("2036-1-1 00:00:00 UTC") if (reference_time = .delete(:expires)) # The time given but in the year 2036 year = @stable_s3_expire_at.year expires_at = Time.parse(reference_time.utc.strftime("#{year}-%m-%d %T UTC")) else expires_at = @stable_s3_expire_at end s3_signed_url_for_key(url, expires: expires_at) end |