[RUBY] Save images to Amazon S3 (Local / Heroku)

Introduction

A memorandum for myself

Procedure (Local)

Gem installation

Gemfile


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

Change save destination

config/environments/development.rb


config.active_storage.service = :local 

#Changed to below
config.active_storage.service = :amazon

Added to storage.yml

config/storage.yml


amazon:
 service: S3
 access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
 secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
 region: ap-northeast-1
 bucket:Bucket name

Set environment variables

Terminal


#After Catalina
% vim ~/.zshrc

[insert mode]
export AWS_ACCESS_KEY_ID="Access key ID"
export AWS_SECRET_ACCESS_KEY="Secret access key"
[:Save as wq]

#Command to reflect
% source ~/.zshrc

Procedure (Heroku)

Change save destination

config/environments/production.rb


config.active_storage.service = :local 

#Changed to below
config.active_storage.service = :amazon

Set environment variables

Terminal


heroku config:set AWS_ACCESS_KEY_ID="Access key ID"
heroku config:set AWS_SECRET_ACCESS_KEY="Secret access key"

Confirmation command

Terminal


% heroku config

push and reflect

in conclusion

You don't have to make each bucket because you can reuse it.

✔︎

Recommended Posts

Save images to Amazon S3 (Local / Heroku)
Heroku + ActiveStorage + Amazon S3
How to post images on Heroku + CarrierWave + S3
Save twitter icon to s3
[Rails] I can post S3 images in my local environment, but I can't switch to S3 on Heroku.
How to deploy to Heroku from a local docker image
How to download images from AWS S3 (rails, carrierwave)
Steps to deploy to Heroku
Can't upload to heroku