[RUBY] [Rails] How to upload images to AWS S3 using Carrierwave and fog-aws

Target

ezgif.com-video-to-gif.gif

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

You have already registered an AWS account.

The following has been implemented.

Implementation of posting function -Implementation of image posting function using Carrierwave

AWS settings

1. Access the link below

AWS Management Console (https://ap-northeast-1.console.aws.amazon.com/console/home?region=ap-northeast-1)

2. Click Service

スクリーンショット 2020-06-16 19.30.54.png

3. Click ʻIAM`

スクリーンショット 2020-06-16 19.31.06.png

4. Click User

スクリーンショット 2020-06-16 19.31.15.png

5. Click Add User

スクリーンショット 2020-06-16 19.31.33.png

6. User details settings

** ① Enter your user name. (Appropriate) **

** ② Set the access type to programmatic access. ** **

** ③ Click Next Step: Access Restriction ** スクリーンショット 2020-06-16 19.32.08.png

7. Click Attach existing policy directly

スクリーンショット 2020-06-16 19.32.48.png

8.Select ʻAmazonS3FullAccess and click Next Step: Tag`

スクリーンショット 2020-06-16 19.33.41.png

9. Click Next Step: Confirm

スクリーンショット 2020-06-16 19.33.58.png

10. Click Create User

スクリーンショット 2020-06-16 19.34.06.png

11. Copy the access key ID and secret access key, make a note of them, and click service.

スクリーンショット 2020-06-16 19.35.07.png

12. Click S3

スクリーンショット 2020-06-16 19.36.03.png

13. Click Create Bucket

スクリーンショット 2020-06-16 19.36.15.png

14. Set name and region

** ① Enter the bucket name. (Appropriate) **

** ② Set the region to Asia Pacific (Tokyo). ** **

** ③ Click Next. ** **

スクリーンショット 2020-06-16 19.37.06.png

14. Click Next

スクリーンショット 2020-06-16 19.38.19.png

15. Click Next

スクリーンショット 2020-06-16 19.38.26.png

16. Click Create Bucket

スクリーンショット 2020-06-16 19.38.29.png

Implementation

1. Make the key an environment variable

** ① Introduced "gem'dotenv-rails'" **

Gemfile


gem 'dotenv-rails'

Terminal


& bundle

** ② Create a ".env" file directly under the application **

Terminal


$ touch .env 

スクリーンショット 2020-06-02 11.32.54.png

** ③ Edit the .env file **

.env


S3_ACCESS_KEY_ID = 'access key' #Postscript
S3_SECRET_ACCESS_KEY = 'Secret access key' #Postscript

** ④ Edit the .gitignore file **

.gitignore


/.env #Postscript

2. Introduce Gem

Gemfile


gem 'fog-aws'

Terminal


$ bundle

3. Edit ʻimage_uploader.rb`

Comment out storage: file on the 7th line and add the following code.

image_uploader.rb


#Postscript
if Rails.env.development? #For development environment
  storage :file
elsif Rails.env.test? #For test environment
  storage :file
else #For production environment
  storage :fog
end

storage :file ➡︎ Upload the image into the application.

storage :fog ➡︎ Upload the image to S3.

4. Create / edit carrierwave.rb

Terminal


$ touch config/initializers/carrierwave.rb

carrierwave.rb


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

CarrierWave.configure do |config|
  if Rails.env.production? #Upload to S3 in production environment
    config.storage :fog
    config.fog_provider = 'fog/aws'
    config.fog_directory  = 'matsubishi-sample' #Bucket name
    config.fog_public = false
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: ENV['S3_ACCESS_KEY_ID'], #access key
      aws_secret_access_key: ENV['S3_SECRET_ACCESS_KEY'], #Secret access key
      region: 'ap-northeast-1', #region
      path_style: true
    }
  else #Upload within the application if not in production
    config.storage :file
    config.enable_processing = false if Rails.env.test?
  end
end

Recommended Posts

[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
[Rails] How to upload images using Carrierwave
[Rails] How to upload multiple images using Carrierwave
How to download images from AWS S3 (rails, carrierwave)
[Rails] How to delete images uploaded by carrierwave (using devise)
How to save images on Heroku to S3 on AWS
How to post images on Heroku + CarrierWave + S3
How to deploy to AWS using NUXTJS official S3 and CloudFront? With docker-compose
[rails] How to post images
How to implement a circular profile image in Rails using CarrierWave and R Magick
How to test including images when using ActiveStorage and Faker
[Rails API + Vue] Upload and display images using Active Storage
How to set and describe environment variables using Rails zsh
upload images to refile heroku S3
[rails6.0.0] How to save images using Active Storage in wizard format
Upload Rails app image file to S3
How to implement image posting using rails
[Rails] How to handle data using enum
Multiple image upload function using Rails Carrierwave
[Android] How to pass images and receive callbacks when sharing using ShareCompat
How to output Excel and PDF using Excella
How to execute and mock methods using JUnit
[Rails] How to create a graph using lazy_high_charts
How to output CSV created by Rails to S3
How to build API with GraphQL and Rails
How to link images using FactoryBot Active Storage
[Rails] How to get success and error messages
Rails scope anti-patterns and how to eliminate them
CarrierWave Upload multiple images
How to write Rails
How to uninstall Rails
How to convert A to a and a to A using AND and OR in Java
[Rails] How to edit and customize devise view and controller
[Rails] Create sitemap using sitemap_generator and deploy to GAE
[Rails 6] Add images to seed files (using Active Storage)
Rails learning How to implement search function using ActiveModel
(Ruby on Rails6) How to create models and tables
[Rails] How to install a decorator using gem draper
Try to implement tagging function using rails and js
How to set environment variables when using Payjp with Rails
How to input multiple images at once using rake task
[Rails] How to define macros in Rspec and standardize processing
Prevent operations! How to securely update Rails manually using transactions
[Rails] Differences between redirect_to and render methods and how to output render methods
How to deploy jQuery in your Rails app using Webpacker
How to join a table without using DBFlute and sql
How to run React and Rails on the same server
[Rails] I tried to implement "Like function" using rails and js
[AWS / S3] After all, how do you upload multiple files?
How to deploy a Rails application on AWS (article summary)
[AWS] How to check logs
[Rails] How to use enum
[Rails] How to install devise
How to minimize Java images
[Rails] How to use enum
How to read rails routes
How to use rails join
How to terminate rails server
How to write Rails validation
How to write Rails seed