[RUBY] How to post images on Heroku + CarrierWave + S3

1. Work flow

First of all, create an S3 bucket. The reason to do it first is to set the settings decided in S3 for the Rails application developed locally later. Because you enter it.

How to make an S3 bucket, but if you look at the reference materials, it will proceed without problems.

As a flow,

  1. First, create a user with IAM. (Use the access key ID and secret access key created here in the carrierwave settings.)
  2. Create an S3 bucket. (Make a box to store images)
  3. Install gem fog-aws and create a configuration file etc.

I will proceed like this.

1. Create an IAM user

First, when you register an account on AWS, an account with extremely strong authority called root is created. Officially, for security reasons, we don't recommend using the root user on a daily basis, so we'll use a service called IAM to create a user with different privileges for everyday use.

In other words, even if it is your own AWS account, it is an image of creating and managing another user with small privileges. * The creation method is described in the link written at the end.

2. Create an S3 bucket

Create a box to store the image. The reference material states that it can be accessed as a root user, but IAM users could also do it. The settings here were very carefully summarized in the reference material, so I think that there is no problem if you use it as it is.

Reference material

These are the above two reference links. You can do it according to the street here. [Rails] Carrier Wave Tutorial

3. Various Rails settings with fog-aws installed

fog-aws is OK with recognition of gem that makes it easy when using an external storage service

First install fog-aws

Gemfile

gem 'fog-aws'

Run bundle install

Next, set in Rails which environment to use: fog (S3).

app/uploaders/image_uploader


  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick  <=Uncomment here.

  # Choose what kind of storage to use for this uploader:
By adding this If statement, the location to save is specified for each environment.
In this case, use S3 only in the production environment.

  if Rails.env.development?
    storage :file
  elsif Rails.env.test?
    storage :file
  else
    storage :fog
  end

  ....

Next, set the IAM user information and S3 information created in advance on AWS. First, how to create the configuration file carrierwave.rb`` config/initializers. If you put it in this location, it will be judged as necessary for initialization and will be loaded automatically, so you can create it.

touch config/initializers/carrierwave.rb

Create a file with the above command and describe the settings.

config/initializers/carrierwave.rb

unless Rails.env.development? || Rails.env.test? <=Works only in production environments.
  require 'carrierwave/storage/abstract'
  require 'carrierwave/storage/file'
  require 'carrierwave/storage/fog'

  CarrierWave.configure do |config|
    config.storage :fog
    config.fog_provider = 'fog/aws'
    config.fog_directory = 'carrierwave-test-app' <=Enter the bucket name of the created S3
    config.fog_credentials = {
        provider: 'AWS',
        aws_access_key_id: Rails.application.credentials.dig(:aws, :access_key_id), #Access of the first copied IAM user_key
        aws_secret_access_key: Rails.application.credentials.dig(:aws, :secret_access_key), #First copied IAM user aws_secret_access_key
        region: 'ap-northeast-1' <=Fill in the region of the created S3.
    }
  end
end

If you explain in detail where you put comments

unless Rails.env.development? || Rails.env.test? <=Works only in production environments.

This is as it is.

config.fog_directory = 'carrierwave-test-app' <=Enter the bucket name of the created S3

It's a good idea to go to the S3 page you created first and check it. Also check for typos.

aws_access_key_id: Rails.application.credentials.dig(:aws, :access_key_id), #Access of the first copied IAM user_key
aws_secret_access_key: Rails.application.credentials.dig(:aws, :secret_access_key), #First copied IAM user aws_secret_access_key

Here, the IAM user's access_key and aws_secret_access_key are specified in credentials.yml.enc. The reason is that if you enter it directly here, you will not be able to share the file on github etc., so I put it in an environment variable so that it can not be understood from the outside.

This time I used credentials.yml.enc, but in the reference article dotenv-rails is used. In short, it is good to suppress the purpose of making the contents of the key invisible from the outside.

■ Reference materials [Rails] Let's manage environment variables by understanding how to install and use dotenv-rails!

Test in a production environment.

So far, after setting up, I will test whether the image is uploaded to S3 in the production environment.

Since the setting method of the production environment is described in Rails (p274 ~) on site, please refer to that.

First asset precompile.

Terminal

bin/rails assets:precompile

Set up a static file distribution server

cofig/environments/production.rb

config.public_file_server.enabled = ENV[:RAILS_SERVE_STATIC_FILES]

I want this to be true

~/.bash_profile

export RAILS_SERVE_STATIC_FILES = 1

Will be true.

Creating a database for the production environment

Since rails db: create only creates DB for development and test by default, Also create for production.

For postgresql

createuser -d -P app name
You will be asked for the password, so enter it twice, including for confirmation, and finish.

Work to reflect in config/database.yml

~/.bash_profile

export App name_DATABASE_PASSWORD =Password set

Now let's start the server in the production environment.

Terminal

bin/rails s --environment=production

This should bring the server up. This area is written in detail when I buy Rails on site and read it. Field Rails

If you post the image here and it is displayed correctly, it should have been uploaded to S3 if the settings are correct.

Thank you for reading for a long time. If you have any mistakes, please let me know.

Other

Here, I talked about the cooperation between carrierwave and S3, so I have not touched on settings such as heroku. I will post a reference article below, so please use it.

■ Deploy Rails app [For beginners] How to reliably deploy rails apps using heroku [Definitive Edition] ■ How to copy master_key (last ride) How to enable posting of images on Heroku (ActiveStorage + Amazon S3)

Recommended Posts

How to post images on Heroku + CarrierWave + S3
How to save images on Heroku to S3 on AWS
[rails] How to post images
How to deploy on heroku
upload images to refile heroku S3
How to redo a deployment on Heroku
[Ruby on Rails] How to use CarrierWave
How to publish an application on Heroku
Save images to Amazon S3 (Local / Heroku)
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
Note how to rollback Mysql deployed on Heroku
[Rails] How to upload multiple images using Carrierwave
[Rails] I can post S3 images in my local environment, but I can't switch to S3 on Heroku.
How to connect to ClearDB from Sequel Pro on Heroku
[Ruby On Rails] How to reset DB in Heroku
How to handle uploaded images
How to minimize Java images
How to deploy a simple Java Servlet app on Heroku
[Rails] How to delete images uploaded by carrierwave (using devise)
How to check the database of apps deployed on Heroku
How to deploy jQuery on Rails
How to deploy Laravel on CentOS 7
How to "hollow" View on Android
[Java] How to update Java on Windows
How to install ImageMagick on Windows 10
How to use Java HttpClient (Post)
How to deploy Bootstrap on Rails
How to run JavaFX on Docker
How to use Bio-Formats on Ubuntu 20.04
How to connect Heroku and Sequel
How to install MariaDB 10.4 on CentOS 8
Rails on Tiles (how to write)
How to install WildFly on Ubuntu 18.04
Deploy Rails on Docker to heroku
How to build vim on Ubuntu 20.04
Upload multiple images to Cloudinary on Active Storage and publish to Heroku
How to check Java installed on Mac
A memorandum on how to use Eclipse
I want to display background-ground-image on heroku.
How to detect microphone conflicts on Android
How to switch thumbnail images with JavaScript
How to install Eclipse (Photon) on Mac
How to install production Metabase on Ubuntu
How to switch Java versions on Mac
How to install beta php8.0 on CentOS8
Deploy to Heroku [Ruby on Rails] Beginner
How to change the timezone on Ubuntu
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Heroku] How to solve when an error is displayed on git push heroku master
POST images from Android to PHP using Retrofit
[Ruby on Rails] How to display error messages
How to deploy a container on AWS Lambda
How to configure ubuntu to be used on GCP
How to add / remove Ruby on Rails columns
How to make rbenv recognize OpenSSL on WSL
How to install network drivers on standalone Ubuntu
How to output CSV created by Rails to S3
Introducing New Relic to Rails apps on Heroku
How to install NVIDIA driver on Ubuntu 18.04 (Note)
Ssh login to the app server on heroku