In Rails, what URL does rails_blob_path or url_for give for an Active Storage file in redirect mode, and how long does it stay valid?
answer
- /rails/active_storage/blobs/redirect/...
- signed_id in the path
- 302 to a signed service URL
- service_urls_expire_in: 5 minutes
- urls_expire_in defaults to nil
basics
~20 sIn redirect mode the helper returns a stable Rails URL containing the blob's signed_id. Requesting it makes Rails answer 302 with a signed service URL that expires after service_urls_expire_in (5 minutes); the Rails URL itself never expires unless urls_expire_in is set.
solid answer
~30 sRedirect is the default delivery mode (`config.active_storage.resolve_model_to_route = :rails_storage_redirect`). `url_for(inspection.photos.first)` or `rails_blob_path(photo, disposition: "attachment")` returns an app URL like `/rails/active_storage/blobs/redirect/<signed_id>/kitchen.jpg`. `ActiveStorage::Blobs::RedirectController` verifies the signed id, sets `Cache-Control` for `service_urls_expire_in` (5 minutes by default) and redirects with 302 to `blob.url`, a presigned URL for a private service that expires after those 5 minutes. The signed id in the Rails URL has no expiry unless you set `config.active_storage.urls_expire_in` or pass `expires_in:`, so the app URL works for anyone who has it, and the default controllers check no login. Variants use the representations route the same way. A bad signature answers 404.
code
erb · 4 lines<% inspection.photos.each do |photo| %>
<%= image_tag photo.variant(resize_to_limit: [800, 800]) %>
<%= link_to "Download original", rails_blob_path(photo, disposition: "attachment") %>
<% end %>go deeper
Recall that url_for or rails_blob_path gives a Rails URL that redirects to the storage service, and that disposition: "attachment" makes a download link.
Explain the two URLs, the 302 from Blobs::RedirectController, service_urls_expire_in versus urls_expire_in, and how variants use the representations route.
Assess the privacy exposure of permanent public app URLs for private files and choose between expiring URLs and authenticated controllers.
Decide a file-access policy (who may see which files, for how long, and how leaks are contained) and map it onto Active Storage's URL settings.
## Two URLs, not one Active Storage deliberately separates the URL your pages contain from the URL the bytes come from. | | App URL | Service URL | |---|---|---| | Built by | `url_for(attachment)`, `rails_blob_path`, `rails_blob_url`, `image_tag` | `blob.url` | | Points at | your Rails app, under `/rails/active_storage` | the storage service (bucket, or Rails' Disk controller) | | Contains | the blob's `signed_id` and filename | a signature generated by the service adapter | | Lifetime | no expiry by default (`urls_expire_in` is `nil`) | `service_urls_expire_in`, 5 minutes by default | In **redirect mode**, the default, the app URL is a level of indirection: it is cheap to render, survives a change of storage service, and only turns into a short-lived service URL when someone requests it. ## The request, step by step For an inspection photo stored in a private S3 bucket: 1. The view renders `image_tag inspection.photos.first`; the route helper resolves the attachment through `resolve_model_to_route` (`:rails_storage_redirect`) to `/rails/active_storage/blobs/redirect/eyJfcmFpbHMi...--3f2a/kitchen.jpg`. 2. The browser requests it. `ActiveStorage::Blobs::RedirectController` runs `ActiveStorage::Blob.find_signed!` on the id; a tampered or expired id answers **404**. 3. The action calls `expires_in ActiveStorage.service_urls_expire_in`, so browsers may cache the redirect for 5 minutes. 4. It responds **302** with `blob.url(disposition: params[:disposition])`: for S3, a presigned GET URL valid for 5 minutes that also fixes the response's content type and `Content-Disposition`. 5. The browser follows the redirect and downloads straight from the bucket. Variants and previews work identically through `/rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename`, handled by `ActiveStorage::Representations::RedirectController`, which also processes the variant on first request. ## Helpers you will use - `url_for(record.attachment)` or `polymorphic_url(blob)`: routes through `resolve_model_to_route`. - `rails_blob_path(blob_or_attachment, disposition: "attachment")`: same route with a disposition, for download links. - `rails_storage_redirect_path` / `rails_storage_proxy_path`: force one mode for a single link regardless of the global setting. - Outside views (a mailer, a job): `Rails.application.routes.url_helpers.rails_blob_url(photo, host: "inspections.example")`, or `only_path: true`. - `blob.url` directly: returns the service URL; only appropriate when you control who receives it and it is used within its lifetime. Active Storage also forces `Content-Disposition: attachment` and a binary content type for risky types listed in `content_types_to_serve_as_binary` (HTML, SVG, XML), and serves inline only types in `content_types_allowed_inline`, to stop uploaded files from running script in your origin. ## The privacy consequence The guide's warning is blunt: **all Active Storage controllers are publicly accessible by default**. The `signed_id` makes the app URL hard to guess, but with `urls_expire_in` at `nil` it is **permanent**. A homeowner who forwards an inspection report email with photo links gives every recipient indefinite access, and a `before_action :require_authentication` in `ApplicationController` does not apply, because the Active Storage controllers do not inherit from it. Ways to tighten it, from least to most work: 1. Set `config.active_storage.urls_expire_in = 1.hour` (or pass `expires_in:` to the helper) so app URLs stop working after a while; pages must then be re-rendered to get fresh links. 2. Keep URLs inside authenticated pages only and accept that a leaked link works until it expires. 3. Turn off the default routes with `config.active_storage.draw_routes = false` and serve files through your own controller that authorizes the user and then redirects to `blob.url`. ## Common misreadings - The 5-minute limit applies to the **service** URL, not the link in the page; reloading the page is not needed for a photo to keep working. - `service_urls_expire_in` also sets how long direct-upload URLs are valid. - A public service (`public: true`) changes step 4: `blob.url` returns a permanent unsigned URL, so the expiry no longer applies at all.
- A user bookmarks a photo URL from the page. Does it still work tomorrow?With defaults, yes. The bookmarked app URL contains a signed id with no expiry, so tomorrow the redirect controller signs a fresh 5-minute service URL and redirects again. Only the service URL expires. Setting `config.active_storage.urls_expire_in` or passing `expires_in:` makes the bookmark fail with 404 after that time.
- Why not embed blob.url directly in the page to save a redirect?The service URL expires after `service_urls_expire_in`, so a page left open or served from a cache shows broken images, and the URL ties the HTML to one storage service. The app URL can be regenerated into a fresh service URL on every request and keeps working when the storage service changes.
Redirect mode works like a hotel front desk: the room number on your booking never changes, but each time you ask, the desk cuts a key card that stops working shortly after.
- The URL from rails_blob_path stops working after five minutes
- Active Storage URLs require the user to be signed in by default
- In redirect mode the Rails app streams the file bytes to the browser
- service_urls_expire_in controls how long links rendered in the page stay valid
- A tampered signed_id in the URL raises a 500 error