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,
IAM
. (Use the access key ID and secret access key created here in the carrierwave settings.)fog-aws
and create a configuration file etc.I will proceed like this.
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.
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.
These are the above two reference links. You can do it according to the street here. [Rails] Carrier Wave Tutorial
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!
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.
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