Help us understand the problem. What is going on with this article?

rails6でcarrierwave実装(aws-s3)

More than 1 year has passed since last update.

rails6でcarrierwaveを実装使用としたら
動機: gem 'fog'で実装しようとしたらエラー出たので
出ないようにした。

プロジェクトを作成

[ターミナル]

mkdir [プロジェクト名]
cd [プロジェクト名]
rails new . -d postgresql
rails db:create

Slimの導入

echo 'gem "slim-rails"' >> Gemfile
echo 'gem "html2slim"' >> Gemfile
bundle
bundle exec erb2slim app/views/layouts/ --delete

rails6 & carrierwave

photoモデルをつくって、imageカラムに画像をアップロード出来るように設定します。

インストール

echo "gem 'carrierwave'" >> Gemfile
echo "gem 'fog-aws'" >> Gemfile
echo "gem 'rmagick'" >> Gemfile
bundle

実装例

rails g scaffold photo image
rails db:migrate
rails generate uploader Image

carrierwaveの設定

/app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base

  # リサイズしたり画像形式を変更するのに必要
    include CarrierWave::RMagick

  # 画像の上限を640x480にする
    process :resize_to_limit => [640, 480]



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

  def store_dir
      "uploads/#{model.class.to_s.underscore}"
  end

  def extension_whitelist
      %w(png jpg)
  end
  # filenameをuuid
  def filename
      "#{secure_token}.#{file.extension}" if original_filename.present?
  end

  protected
    def secure_token
        var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
    end
end

AWS S3の認証情報をcredentialsに追加

rails credentials:edit
aws:
  access_key_id: いつもの
  secret_access_key: いつもの

s3_fog_directory: S3のバケット名

前半で設定した内容を元に設定する
* aws_access_key_id S3用に用意したIAMユーザーのアクセスキー
* aws_secret_access_key S3用に用意したIAMユーザーのシークレットアクセスキー
* region S3のリージョン
* config.fog_directory S3のバケット名

config/initializers/carrierwave.rb
CarrierWave.configure do |config|
  config.fog_credentials = {
    provider: 'AWS',
    aws_access_key_id: Rails.application.credentials.dig(:aws, :access_key_id),
    aws_secret_access_key: Rails.application.credentials.dig(:aws, :secret_access_key),
    region: 'ap-northeast-1'
  }

  config.fog_directory  = Rails.application.credentials.dig(:s3_fog_directory)
  config.cache_storage = :fog
end
app/models/photo.rb
class Photo < ActiveRecord::Base
  mount_uploader :image, ImageUploader
end
app/views/photos/_form.html.slim
= f.file_field :image
app/views/photos/show.html.slim
= image_tag photo.image.to_s

参考文献

https://github.com/carrierwaveuploader/carrierwave

nakano1994
台東区でエンジニアやってます。
https://web-tips.2rta.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした