Upload Rails app image file to S3

1. Create bucket

image.png

image.png

image.png

image.png Copy ARN of IAM user

image.png

Bucket policy


{
    "Version": "2012-10-17",
    "Id": "Policy1544152951996",
    "Statement": [
        {
            "Sid": "Stmt1544152948221",
            "Effect": "Allow",
            "Principal": {
                "AWS": "************①****************"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::************②**********"
        }
    ]
}

Describe the "user's ARN" that you wrote down earlier in (1) above, and the "bucket name" that you created in (2).

2. fog settings

gemfile


gem 'fog-aws'

bundle install

app/uploaders/image_uploader.rb


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

Create a file called carrierwave.rb from the root of your application directly under config / initializers.

config/initializers/carrierwave.rb


require 'carrierwave/storage/abstract'
require 'carrierwave/storage/file'
require 'carrierwave/storage/fog'

CarrierWave.configure do |config|
  if Rails.env.development? || Rails.env.test?
    config.storage = :file
  else
    config.storage = :fog
    config.fog_provider = 'fog/aws'
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
      aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
      region: 'ap-northeast-1'
    }
    config.fog_directory  = 'Bucket name'
    config.asset_host = 'https://s3-ap-northeast-1.amazonaws.com/Bucket name'
  end
end

3. Setting environment variables

ec2 server


#Production environment
$ ssh -i [pem key name].pem ec2-user@[Elastic IP associated with the created EC2 instance]
(Using the downloaded key, ec2-Login as user)
$ sudo vim /etc/environment
#Press i to switch to insert mode and add the following. Do not delete the existing description.
AWS_ACCESS_KEY_ID='Copy the value of Access key ID in the CSV file here'
AWS_SECRET_ACCESS_KEY='Copy the Secret access key value to the CSV file here'
#After editing, press the escape key:Type wq to save and exit

AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are described in the CSV file downloaded when IAM user was created.

ec2 server


#Production environment
#Log out once to apply the edited environment variable.
$ exit
$ ssh -i [pem key name].pem ec2-user@[Elastic IP associated with the created EC2 instance]
#Make sure the environment variables are applied.
$ env | grep AWS_SECRET_ACCESS_KEY
$ env | grep AWS_ACCESS_KEY_ID

4. Automatic deployment

① Push to GitHub ② Execute "bundle exec cap production deploy"

Recommended Posts

Upload Rails app image file to S3
Change the save destination of the image to S3 in the Rails app. Part 2
[Note] Download from S3, upload to S3
upload images to refile heroku S3
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
[Rails] How to upload images to AWS S3 using refile and refile-s3
Image upload
Migration file to add comment to Rails table
How to change app name in rails
[Rails] How to upload images using Carrierwave
Introduced Vue.js to an existing Rails app
Try deploying Rails app to EC2-Part 2 (Deploy)-
How to implement image posting using rails
How to achieve file upload with Feign
Introduced Vuetify to an existing Rails app
Multiple image upload function using Rails Carrierwave
Image file overlay
Downgrade an existing app created with rails 5.2.4 to 5.1.6
How to output CSV created by Rails to S3
[Rails] How to upload multiple images using Carrierwave
Unify the Rails app time zone to Japan time
Rails "How to delete NO FILE migration files"
I tried to implement file upload with Spring MVC
[Heroku] Associate AWS S3 with the deployed Rails app
What to do when rails creates a 〇〇 2.rb file
How to test file upload screen in Spring + Selenium
How to download images from AWS S3 (rails, carrierwave)
[Rails 6] How to set a background image in Rails [CSS]
How to get started with creating a Rails app
[Rails] How to display an image in the view
How to write Rails
Introducing CircleCI to Rails
Introducing Bootstrap to Rails 5
Introducing Bootstrap to Rails !!
[Rails 6] RuntimeError with $ rails s
Introduce Vue.js to Rails
Heroku app moving (rails)
How to uninstall Rails
Can't upload to heroku
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I want to push an app made with Rails 6 to GitHub
Windows10 "rails s" cannot be started. Unable to access localhost: 3000
[Rails] Create API to download files with Active Storage [S3]
How to specify db when creating an app with rails
How to deploy jQuery in your Rails app using Webpacker
Confirm public key ~ Register key on Github ~ Push Rails app to Github
[Rails5] japanMap link How to write parameters in js.erb file
[Rails carrier wave] How to not transition to the error screen even if the image upload is not selected
[Rails Tutorial Chapter 13] Flow when using S3 for image upload in production environment (use .env so as not to publish AWS key)