[Rails] I can post S3 images in my local environment, but I can't switch to S3 on Heroku.

phenomenon

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. .. ..

Cause

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 *****

Settings in the local environment (* Settings on the aws side are omitted)

① Introduce the Gemfile required to use S3

Gemfile.


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

② After adding, enter the application under development in the terminal and execute "bundle install"

Terminal.


% bundle install

③ Update development.rb and storage.yml files

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'] %>

④ Addition of environment variables

Since "AWS_ACCESS_KEY_ID" and "AWS_SECRET_ACCESS_KEY" are set in storage.yml of ③, add environment variables in the application under development.

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

Now you can post S3 images in your local environment !!!!!!: relaxed:
After that, it will be reflected in the production environment (Heroku). : writing_hand:

Reflection in the production environment (Heroku)

① 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

: sparkles :: sparkles :: sparkles :: sparkles: Thank you for your hard work. : sparkles :: sparkles :: sparkles :: sparkles:

Recommended Posts

[Rails] I can post S3 images in my local environment, but I can't switch to S3 on Heroku.
How to post images on Heroku + CarrierWave + S3
I installed Squid on CentOS in my local environment
I want to add devise in Rails, but I can't bundle install
I can't install rails on my mac
Save images to Amazon S3 (Local / Heroku)
How to save images on Heroku to S3 on AWS
I can't overwrite information on columns in rails console ...
When I tried to reproduce Progate's Rails app on my PC (local environment), I got stuck at the image display
[Ruby On Rails] How to reset DB in Heroku
I can deploy to heroku, but I get an error and can't open it [Cause: MySQL]
I can't log in on Heroku when I introduce Basic authentication.
[Programming beginner] What to do when rails s becomes an error in the local development environment
After learning Progate, I tried to make an SNS application using Rails in the local environment
I want to display the images under assets/images in the production environment
upload images to refile heroku S3
I implemented the multiple image upload function in Rails so that multiple images can be slid for the post
Deploy Rails on Docker to heroku
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
[Error] Switch environment construction to use Ruby on Rails oss (open source)
How to solve the local environment construction of Ruby on Rails (MAC)!
I can deploy Heroku, but I get an error and can't open it
[Rails] I want to reset everything because the data in the local environment is strange! What to do before that
I want to display background-ground-image on heroku.
Deploy to Heroku [Ruby on Rails] Beginner
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
I created a Rails post form, but I can't post it (form tag) / No error occurs