skip to content

What changes when an Active Storage service in config/storage.yml is marked public: true?

level: middleimportance: should knowfreq 30%

answer

  1. permanent unsigned URLs
  2. blob.url returns public_url
  3. S3 uploads with public-read ACL
  4. bucket must allow public reads
  5. mix via has_one_attached service:

basics

~20 s

With public: true, blob.url returns the object's permanent, unsigned URL instead of a short-lived signed one, and S3 uploads are written with a public-read ACL. The bucket must allow public reads, and every file on that service becomes readable by anyone with its address.

solid answer

~40 s

Services are private by default: `blob.url` returns a signed URL valid for `service_urls_expire_in`. Adding `public: true` to a service entry makes `ActiveStorage::Service#url` call `public_url` instead, so S3 and GCS return the object's permanent unsigned URL and the expiry no longer applies. The S3 adapter also adds `acl: "public-read"` to uploads, which needs the `s3:PutObjectAcl` permission and a bucket that allows public ACLs. Redirect-mode app URLs still work; the redirect simply lands on a permanent URL. Files already in the bucket keep their old access until you make each one public. Because one app usually holds both kinds of files, you define a separate public service and choose it per attachment with `has_one_attached :cover_photo, service: :public_s3`, keeping private inspection photos on the default private service.

code

ruby · 5 lines
ruby
listing.cover_photo.url
# => "https://listing-covers-production.s3.eu-central-1.amazonaws.com/x9k2..."   (permanent)

inspection.photos.first.url
# => "https://inspection-photos-production.s3...?X-Amz-Expires=300&X-Amz-Signature=..."   (5 minutes)

go deeper

for a junior

Recall that services are private by default and public: true gives permanent URLs anyone can open.

for a middle

Explain how Service#url chooses public_url or private_url, the S3 public-read ACL, and how to keep public and private files on separate services with service:.

for a senior

Judge which files may be public, plan converting an existing bucket, and keep revocable or personal files off public services.

for a principal

Set the classification policy for user-generated files and the storage layout that enforces it.

## Private by default Every Active Storage service is **private** unless configured otherwise. For a private service, `ActiveStorage::Blob#url` asks the adapter for a signed URL that expires after `service_urls_expire_in` (five minutes by default); for S3 that is a presigned GET with the response's content type and disposition baked in. That is why pages normally link to Rails' redirect URL rather than to the bucket. ## What public: true switches `public: true` is read by the Disk, S3 and GCS adapters' constructors and exposed as `public?`. `ActiveStorage::Service#url` then branches: ```ruby if public? public_url(key, **options) else private_url(key, **options) end ``` | Behaviour | Private service | `public: true` service | |---|---|---| | `blob.url` | signed, expires after `service_urls_expire_in` | permanent, unsigned object URL | | S3 upload options | as configured | `acl: "public-read"` added automatically | | Bucket requirement | private | must allow public reads (and, on S3, public ACLs) | | Who can fetch a known URL | holder, until expiry | anyone, forever | | Redirect-mode app URL | redirects to a fresh signed URL | redirects to the permanent URL | The redirect still happens when you render `url_for(attachment)`, because route helpers do not care whether the service is public. You may render `blob.url` directly for public files, which saves the hop through Rails at the cost of tying the page to the bucket's address. ## Mixing public and private files Most products hold both. A property-inspection platform might publish a marketing cover image per listing while inspection photos stay private. Define two services and choose per attachment: ```yaml inspections_s3: service: S3 bucket: inspection-photos-<%= Rails.env %> region: eu-central-1 public_s3: service: S3 bucket: listing-covers-<%= Rails.env %> region: eu-central-1 public: true ``` ```ruby class Listing < ApplicationRecord has_one_attached :cover_photo, service: :public_s3 end class Inspection < ApplicationRecord has_many_attached :photos # global default: :inspections_s3 end ``` The `service:` option is validated against `storage.yml` when the class loads, and each blob records `public_s3` or `inspections_s3` in its `service_name`. ## Converting an existing service Flipping `public: true` on a service that already holds files changes only what Rails **generates** and how **new** uploads are written: 1. Existing objects keep their old ACLs, so their permanent URLs fail until each object is made publicly readable, which the guide tells you to do before switching. 2. Bucket-level settings that block public access must be relaxed first; that is configured in the storage provider, not in Rails. 3. Anything previously private on that service becomes public once its objects are opened up, so audit what the service holds. ## When public services are the right choice - **Assets meant for everyone**: listing covers, avatars on public profiles, images embedded in public pages or emails, where permanent URLs let browsers and CDNs cache freely. - **Fewer requests**: no redirect hop and no signing work per view. And when they are not: - Anything with personal data, such as inspection photos showing a home's interior, or documents shared with a single customer. - Files whose access must be revocable: a public URL cannot be withdrawn except by deleting or re-permissioning the object. Public services cover the "everyone may see it" case completely; for "only some people may see it", keep the service private and control access through URL lifetimes or your own controller.

  • Does public: true make Rails skip its redirect controller?
    No. Route helpers such as `url_for(attachment)` still produce the redirect-mode app URL; the controller just redirects to the permanent public URL instead of a signed one. To skip the hop, render `blob.url` directly for files on the public service.
  • Why might uploads to a public S3 service start failing with access denied?
    The S3 adapter adds `acl: "public-read"` to uploads when `public: true` is set. If the credentials lack `s3:PutObjectAcl`, or the bucket blocks public ACLs, the upload is rejected even though plain uploads to the same bucket worked before.

  • public: true lets anonymous users upload files to the service
  • public: true makes existing private objects readable immediately
  • On a public service blob.url still expires after service_urls_expire_in
  • Marking a service public: true also switches its files to proxy mode
  • With a public service Rails stops drawing the Active Storage redirect routes