質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Amazon S3

Amazon S3 (Simple Storage Service)とはアマゾン・ウェブ・サービスが提供するオンラインストレージサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

596閲覧

Rails,AWS(IAM:ユーザ, S3:バケット),herokuでの画像アップロードエラー(AccessDenied)解消

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Amazon S3

Amazon S3 (Simple Storage Service)とはアマゾン・ウェブ・サービスが提供するオンラインストレージサービスです。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2022/09/04 13:22

前提

Rails,AWS(IAM:ユーザ, S3:バケット),herokuでCRUDなシステムを作っています。
画像アップロード機能を実装後の動作確認中に以下のエラーメッセージが発生しました。
申し訳ございませんが、ご確認よろしくお願いします。

実現したいこと

Rails,AWS(IAM:ユーザ, S3:バケット),herokuで新規登録時の
画像アップロードエラー(AccessDenied)解消

発生している問題・エラーメッセージ

(Expected(204) <=> Actual(403 Forbidden)
<Code>AccessDenied</Code><Message>Access Denied</Message>

該当のソースコード

gemfile

1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '2.6.9' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' 7gem 'rails', '~> 6.1.6' 8# Use sqlite3 as the database for Active Record 9#gem 'sqlite3', '~> 1.4' 10# Use Puma as the app server 11gem 'puma', '~> 5.0' 12# Use SCSS for stylesheets 13gem 'sass-rails', '>= 6' 14# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 15gem 'webpacker', '~> 5.0' 16# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 17gem 'turbolinks', '~> 5' 18# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 19gem 'jbuilder', '~> 2.7' 20# Use Redis adapter to run Action Cable in production 21# gem 'redis', '~> 4.0' 22# Use Active Model has_secure_password 23# gem 'bcrypt', '~> 3.1.7' 24 25# Use Active Storage variant 26# gem 'image_processing', '~> 1.2' 27 28# Reduces boot times through caching; required in config/boot.rb 29gem 'bootsnap', '>= 1.4.4', require: false 30 31group :development, :test do 32 gem 'sqlite3', '~> 1.4' 33 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 34 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 35 36 gem 'rspec-rails' 37 38 gem 'factory_bot_rails' 39 40end 41 42group :development do 43 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 44 gem 'web-console', '>= 4.1.0' 45 # Display performance information such as SQL time and flame graphs for each request in your browser. 46 # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md 47 gem 'rack-mini-profiler', '~> 2.0' 48 gem 'listen', '~> 3.3' 49 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 50 gem 'spring' 51 52 gem "rails-erd" 53 54end 55 56group :production do 57 gem 'pg' 58end 59 60group :test do 61 # Adds support for Capybara system testing and selenium driver 62 gem 'capybara', '>= 3.26' 63 gem 'selenium-webdriver', '>= 4.0.0.rc1' 64 # Easy installation and use of web drivers to run system tests with browsers 65 gem 'webdrivers' 66end 67 68# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 69gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 70 71gem 'devise' 72 73gem 'ransack' 74 75gem 'kaminari' 76 77gem 'carrierwave', '~> 2.0' 78 79gem 'mini_magick' 80 81gem 'fog-aws' 82gem "aws-sdk-s3", require: false 83 84gem 'dotenv-rails'

carrierwave.rb

1require 'carrierwave/storage/abstract' 2require 'carrierwave/storage/file' 3require 'carrierwave/storage/fog' 4 5CarrierWave.configure do |config| 6 config.storage :fog 7 config.fog_provider = 'fog/aws' 8 config.fog_directory = 'mportfolio-bucket' 9 config.fog_credentials = { 10 provider: 'AWS', 11 aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], 12 aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], 13 region: ENV['AWS_DEFAULT_REGION'], 14 path_style: true 15 } 16 17end

storage.yml

1test: 2 service: Disk 3 root: <%= Rails.root.join("tmp/storage") %> 4 5local: 6 service: Disk 7 root: <%= Rails.root.join("storage") %> 8# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) 9amazon: 10 service: S3 11 access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> 12 secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> 13 region: ap-northeast-1 14 bucket: 自分のバケット名

production.rb

1config.active_storage.service = :amazon

image_uploader.rb

1class ImageUploader < CarrierWave::Uploader::Base 2 # Include RMagick or MiniMagick support: 3 # include CarrierWave::RMagick 4 include CarrierWave::MiniMagick 5 6 # Choose what kind of storage to use for this uploader: 7 # storage :file 8 # storage :fog 9 if Rails.env.development? 10 storage :file 11 elsif Rails.env.test? 12 storage :file 13 else 14 storage :fog 15 end 16 17 # Override the directory where uploaded files will be stored. 18 # This is a sensible default for uploaders that are meant to be mounted: 19 def store_dir 20 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 21 end 22 23 # Provide a default URL as a default if there hasn't been a file uploaded: 24 def default_url(*args) 25 # # For Rails 3.1+ asset pipeline compatibility: 26 ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 27 # 28 # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 29 end 30 31 version :thumb do 32 process resize_to_fit: [50, 50] 33 end 34 35end

イメージ説明

イメージ説明

イメージ説明

試したこと

下記2リンク手順参考にAWS(IAM:ユーザ, S3:バケット)などの設定後、
heroku上にアプリデプロイを行い、画像アップロードの動作確認(新規登録)を実施。

・Rails, Laravel(画像アップロード)向けAWS(IAM:ユーザ, S3:バケット)の設定
https://qiita.com/nobu0717/items/4425c02157bc5e88d7b6

・【初学者必見】 HerokuとAWS S3を連携して投稿画像を保持する方法
https://qiita.com/jibiking/items/0e8c1d826271ac9e4a7d#%E9%96%8B%E7%99%BA%E7%92%B0%E5%A2%83--%E5%89%8D%E6%8F%90%E6%9D%A1%E4%BB%B6

補足情報(FW/ツールのバージョンなど)

上記2リンク手順でAWS(IAM:ユーザ, S3:バケット)などの設定後、
heroku上にアプリデプロイを行い、画像アップロードの動作確認(新規登録)を行ったところエラー画面が表示。
ただ、エラー後にもう一度herokuにログインすると新規追加は成功されており(画像も表示)、
s3にも画像は保存されてはいます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

解決いたしました、ありがとうございます。

投稿2022/09/04 14:36

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問