Setup:
- Declare an Amazon Active Storage service in
config/storage.yml
amazon:
service: S3
access_key_id: ""
secret_access_key: ""
bucket: ""
region: "" # e.g. 'us-east-1'
-
Use this Amazon service in production:
config.active_storage.service = :amazon
In:config/environments/production.rb
-
Add the AWS gem:
gem "aws-sdk-s3", require: false
Attach a file to a record
-
Use
has_one_attached
orhas_many_attached
macros in your model:class User < ApplicationRecord has_one_attached :profile_picture end
-
Call
profile_picture.attach
to attach a picture to an existing user:user.profile_picture.attach(params[:profile_picture])
Your profile picture image is now saved to Amazon S3!