carrierwaveでS3に画像をアップロードしたい(rails)
carrierwaveでS3に画像をアップロードを実装中にエラーが発生し、どうしても解決できないため質問させていただきます。
お力を貸していただきたいです。
参考サイト1
※上手くいかなかったので、いろいろなサイトを参考しています。
開発環境:docker
ruby:2.5.1
rails:5.2.3>
発生している問題・エラーメッセージ
画像をcreateする際に「getaddrinfo: Name or service not known (SocketError)」とエラーが発生します。
carrierwave.rb
CarrierWave.configure do |config| # if Rails.env.production? # config.storage :fog config.fog_provider = 'fog/aws' config.fog_directory = ENV['AWS_S3_BUCKET'] config.fog_credentials = { provider: 'AWS', aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: ENV['AWS_S3_REGION'], path_style: true } config.cache_storage = :fog config.fog_public = false config.fog_attributes = { cache_control: "max-age=#{365.days.to_i}" } # else # config.storage :file # config.enable_processing = false if Rails.env.test? # end end CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:].\-+]/
picture_uploader.rb
class PictureUploader < CarrierWave::Uploader::Base include CarrierWave::RMagick storage :fog def store_dir "uploads/#{model.id}" end process resize_to_limit: [1280, 670] version :thumb do process :resize_to_limit => [300, 300] end version :thumb50 do process :resize_to_limit => [100, 100] end def extension_whitelist %w(jpg jpeg png) end def filename "#{secure_token(10)}.#{file.extension}" if original_filename.present? end protected def secure_token(length = 16) var = :"@#{mounted_as}_secure_token" model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length / 2)) end end
Gemfile
gem 'carrierwave' gem 'rmagick' gem 'fog-aws' gem 'fog', '1.41' gem 'dotenv-rails' gem 'mini_magick'
recipes_controller.rb
class RecipesController < ApplicationController before_action :set_target_recipe, only: %i[show edit update destroy] before_action :authenticate_user! def new @recipe = Recipe.new end def create recipe = current_user.recipes.create(recipe_params) if recipe.save flash[:notice] = "「#{recipe.title}」を作成しました。" redirect_to recipe else flash[:recipe] = recipe flash[:error_messages] = recipe.errors.full_messages redirect_back(fallback_location: recipe) end end def index @recipes = params[:tag_id].present? ? Tag.find(params[:tag_id]).recipes : Recipe.all @recipes = @recipes.page(params[:page]) end def show @comment = Comment.new(recipe_id: @recipe.id) @comments = @recipe.comments @like = Like.new end def edit end def update if @recipe.update(recipe_params) redirect_to @recipe else flash[:recipe] = @recipe flash[:error_messages] = @recipe.errors.full_messages redirect_back(fallback_location: @recipe) end end def destroy @recipe.destroy redirect_to recipes_path, flash: { notice: "「#{@recipe.title}」が削除されました。"} end private def recipe_params params.require(:recipe).permit(:title, :picture, :picture_cache , :body, tag_ids: []) end def set_target_recipe @recipe = Recipe.find(params[:id]) end end
※recipe_paramsのpicture_cacheを削除しても,エラーが発生します。
.env
AWS_ACCESS_KEY_ID='////' AWS_SECRET_ACCESS_KEY='////' AWS_S3_REGION='us-east-2' AWS_S3_BUCKET='////'
recipe.rb
class Recipe < ApplicationRecord mount_uploader :picture, PictureUploader ....
Dockerfile
FROM ruby:2.4.5 RUN apt-get update -qq && apt-get install -y build-essential nodejs RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - \ && apt-get install -y nodejs RUN mkdir /yumyumkitchen WORKDIR /app COPY Gemfile /app/Gemfile COPY Gemfile.lock /app/Gemfile.lock RUN bundle install COPY . /app
docker-compose.yml
version: '3' services: web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/app ports: - 3000:3000 env_file: .env depends_on: - db tty: true stdin_open: true db: image: mysql:5.7 volumes: - db-volume:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: password volumes: db-volume:
試したこと
docker-compose buildをし直したりしていますが...
12/1修正後carrierwave.rb
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 = ENV['AWS_S3_BUCKET'] config.asset_host = 'https://s3.amazonaws.com/railspotoforio' config.fog_credentials = { provider: 'AWS', aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: ENV['AWS_S3_REGION'], path_style: true } config.cache_storage = :fog config.fog_public = false config.fog_attributes = { cache_control: "max-age=#{365.days.to_i}" } end CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:].\-+]/
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/01 13:49
2019/12/03 03:34