đź’ľ
Cheat Sheet
  • Cheat Sheet
  • Git
    • Initialize a new project
    • Make a new commit
    • Revert the most recent commit
    • Create a new branch
    • Setup terminal prompt to show the current git branch
    • Merge a branch into the current branch
    • Push a new branch up to GitHub (if it doesn’t exist there yet)
    • Pull a new branch from to GitHub to a second computer (if it doesn’t exist there yet)
    • Go back to an older commit and re-work from there
    • Delete a git branch
  • Rails
    • Setup Rails 8 w/ SQLite & Solid Cable, Cache & Queue
    • Stripe Pay Gem
      • Running Stripe Test Mode Locally
    • Setup new Rails app
      • Start a new Rails App (v8)
      • Start a new Rails App (v7)
      • Clone Instrumental Template to a new app
    • Rails 7 Turbo/Hotwire Cheatsheet
    • ActiveStorage with AWS S3
    • Live reloading with webpack-dev-server
    • Sidekiq & Redis
      • Setup Sidekiq in a Rails 8 app
      • Clear sidekiq queue
      • Start jobs worker
    • Setup Solid Queue/Cable/Cache in Rails 8 to share a single database
    • Run rspec tests
      • ZipMessage Tests Commands
      • Can't run rspec tests: ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR: relation "thing"
    • Acts As Tenant (using Rails console)
    • Reset database & re-run migrations
    • Ultrahook for testing email to app
    • Troubleshooting
      • Manage node versions using NVM
      • Webpacker bugs
      • Tailwind updates not showing
      • Fix orphaned migrations
      • rspec error: session not created: This version of ChromeDriver only supports Chrome version
      • “PG::ConnectionBad” error when running local rails server
        • Postgres not started (PG:ConnectionBad error)
    • OLD
      • Start a new Rails app (v6)
        • Install Tailwind CSS on a Rails Project
        • Install Stimulus.js on Rails
        • Install RSpec, Capybara, FactoryBot
        • Install Devise Masqerade
    • Hosting
      • Hatchbox.io + Digital Ocean
      • Fly.io rails app hosting
      • Render rails app hosting
      • Set up a Rails site on Heroku
        • Fix Sidekiq on Heroku
        • Setup app on Heroku for a Client
    • Edit Rails Credentials
    • Run subdomains on local
  • Misc
    • Terminal shortcuts
    • ngrok for tunneling
    • Set up a new Mac
    • Mailcatcher
  • Statamic
  • Forge / Statamic
    • Run Statamic (for Clarityflow) locally
    • Set up to SSH into Forge server
    • Production deploy failed. Resolve by SSH & resolve git conflicts.
  • Old (not using anymore)
    • Heroku
      • Push to Heroku
        • Can't push to heroku. Updates were rejected because the remote contains work you dont have locally
      • Create a Heroku App from command line
      • Set up remotes for staging and production apps
      • Run the Rails Console on Heroku
      • Migrate database on Heroku
      • Make database backups
      • Restart Heroku Dynos
      • Point a root domain (non-subdomain) to heroku
    • Middleman
      • Start a new middleman site from my template
    • Jekyll
      • Start a new Jekyll site from my template
      • Run and work on my Jekyll site
    • Netlify
      • Setup a Jekyll site on Netlify
      • Push to Staging & Production on Netlify
    • Laravel
      • Start a new Laravel app
      • Setup a Mac for running Laravel
    • Design
      • Icomoon Fonts in Rails
    • Sync Sublime Text settings across macs
    • Run a rails project on a new mac for the first time
    • Customize terminal prompt for bash
  • Cursor
    • .cursorrules for Rails + Linear issue writing
  • Mac Environment
    • Install Homebrew for both arm64 and x86
    • Installing Ruby
    • Customize terminal prompt for zsh to show git branch
    • Open terminal in arm64 or Rosetta mode
    • Install Homebrew
Powered by GitBook
On this page
  • Steps for setting up S3 for staging and production:
  • Setup config/storage.yml
  • Create the bucket(s) in S3
  • Setup IAM Policy
  • Create IAM User
  • Edit the User's security credentials & and save to credentials
  • Update config/staging & production.rb active_storage setting
  • Add S3 Gem
  • Update CORS configuation
  1. Rails

ActiveStorage with AWS S3

How to setup ActiveStorage with S3 in Rails 7

PreviousRails 7 Turbo/Hotwire CheatsheetNextLive reloading with webpack-dev-server

Last updated 8 months ago

Follow Chris Oliver's 2023 tutorial here:

Steps for setting up S3 for staging and production:

Setup config/storage.yml

  • Uncomment the amazon block

    • Add public: true to that block (so that S3 files are always visible to public viewers, which is the case most of the time... Unless you're building an app where S3 files should be protected for each user).

    • Update the bucket name to be bucket: appname-<%= Rails.env %>

Create the bucket(s) in S3

  • I like to name them, so that it's easy to define dynamically in config/storage.yml:

    bucket: appname-<%= Rails.env %>

    So name the S3 buckets:

    • appname-staging

    • appname-production

  • Select "ACLs Enabled"

    • Note: In Chris Oliver's video, near the beginning he mistakenly checked 3 boxes, but around 14:00 in the video, he fixed it to check only the bottom 2 boxes.

Leave everything else set to default, then "Create".

Setup IAM Policy

  • Service: S3 (only)

  • Access level: Check all of the following:

    • List > ListBucket

    • Read > GetObject

    • Write > PutObject

    • Write > DeleteObject

    • Permissions Management > PutObjectAcl

  • Resources:

    • Set it to "Specific"

  • Name the policy: appname-s3 then "Create" policy.

Create IAM User

  • Username: appname

    • Leave option for giving access to AWS console to unchecked.

  • In Step 2, Permissions, select "Attach policies directly"

    • Search for the policy name we just created (appname-s3) and check its box

"Create user"

Edit the User's security credentials & and save to credentials

Click the user that you just created -> "Security Credentials"

  • Access Keys -> "Create Access Key"

  • Select "Application running outside AWS"

    • skip "tag description"... click "Create access key"

  • Save the access key and secret key to both staging and production credentials

    • (in this example, we're using the same AWS user and policy for both staging and production, which is fine in most cases).

Update config/staging & production.rb active_storage setting

Open config/environments/staging.rb and config/environments/production.rb and update the following line to :amazon:

config.active_storage.service = :amazon

Add S3 Gem

Add the following gem to gemfile:

gem "aws-sdk-s3", require: false

Also ensure that the image_processing gem is in gemfile (it should already be there)

bundle install

Update CORS configuation

  • In S3 -> Edit each of the buckets you've created -> "Permissions" tab

  • Scroll all the way down to "Cross-origin resource sharing (CORS)"

    • (not to be confused with "Bucket Policy")

    • Edit the "AllowedOrigins" to include:

      • http://127.0.0.1:3000

      • The The URL of the staging or production domain (whichever S3 bucket this is)

      • Optional: The temporary URL of the domain on heroku (or whatever hosting service you're using).

For reference, see official rails guide here:

In "Block public access" settings, check only the bottom 2 boxes. It should look like this:

Go to IAM -> Policies -> "Create a policy"

Then add 2 lines each for production and staging buckets. Name the bucket name, and then name the bucket name and append /*, like this:

Go to IAM -> Users -> Create a User

It should look something like this:

https://guides.rubyonrails.org/active_storage_overview.html#s3-service-amazon-s3-and-s3-compatible-apis
https://share.cleanshot.com/YyxCMyVD
here
https://share.cleanshot.com/Lrhtqt4r
here
Following instructions from the official rails guide here
Copy the example S3 CORS configuration from rails guides here
https://share.cleanshot.com/kNJd7jgV