I was able to successfully store images in S3 in the local environment! !! However, even if I merge it on Heroku side, the image is stored on Heroku's DB for some reason. .. .. There is no error. .. I wonder why. .. ..
It's a matter of course when I think about it now, but ... The cause was that ActiveStorage was not set in the production (Heroku) file .
type | environment | setting file |
---|---|---|
Development environment | Local environment | development.rb |
Production environment | Heroku | production.rb(⇦ I didn't put the setting here) |
***** Leave the procedure so that you do not make the same mistake *****
Gemfile.
gem "aws-sdk-s3", require: false
Terminal.
% bundle install
config/environments/development.rb
#Change settings to save on S3
# config.active_storage.service = :local
config.active_storage.service = :amazon
config/storage.yml
#Added S3 settings
amazon:
service: S3
region : ap-southeast-1 #Check and describe the region on the aws side
bucket : furimabucket #Check the bucket name on the aws side and describe it
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
Terminal.
% vim ~/.zshrc
Enter edit mode with "i" and register two variables
Terminal.
export AWS_ACCESS_KEY_ID="Access key ID value"
export AWS_SECRET_ACCESS_KEY="Secret access key value"
After editing, exit with [ESC] →: wq Then, reload the environment variable configuration file
Terminal.
% source ~/.zshrc
① First of all, don't forget to update production.rb : point_up: ** storage.yml ** has not changed.
config/environments/production.rb
#Set to be saved in S3 even in the production environment
config.active_storage.service = :amazon
② Log in to Heroku at the terminal
Terminal.
% heroku login --interactive
③ Set environment variables even in the production environment If you want to check the environment variables, "% heroku config"
Terminal.
% heroku config:set AWS_ACCESS_KEY_ID="Access key ID value"
% heroku config:set AWS_SECRET_ACCESS_KEY="Secret access key value"
④ Then commit and complete
Terminal.
% git add .
% git commit -m "Storage change (S3)"
% git push heroku master