※他質問サイトでも同様の質問をしています。解決した場合はそちらも更新します。よろしくお願いします。
前提
railsでDM機能を作成しています。
ActiveStorageを使用してS3に画像ファイルをアップロードし、画像をブラウザに表示したいのですが、実装中に以下の問題が発生しました。
ActiveStorage、S3のセットアップは以下を参考にして設定しました。
https://zenn.dev/kurao/articles/ce8e583450c76e
以前までは画像が保存できないエラーが生じていました。
https://teratail.com/questions/3aylzv3ia4g0s8
実現したいこと
フォームから送信した画像をブラウザに表示したいです。
送信したメッセージはLINEのように蓄積されていきます。
発生している問題・エラーメッセージ
S3のオブジェクトには画像が保存されているのですが、ブラウザに表示されない問題が発生しています。
該当のソースコード
config/environment/development.rb
1Rails.application.configure do 2config.web_console.whitelisted_ips = '0.0.0.0/0' 3 # ... 4 5 config.active_storage.service = :amazon 6 7 # ... 8 9 Rails.application.routes.default_url_options[:host] = 'localhost' 10 Rails.application.routes.default_url_options[:port] = 3000 11end
db/migrate/(日付)_create_active_storage_tables.active_storage.rb
1class CreateActiveStorageTables < ActiveRecord::Migration[5.2] 2 def change 3 create_table :active_storage_blobs do |t| 4 t.string :key, null: false 5 t.string :filename, null: false 6 t.string :content_type 7 t.text :metadata 8 t.bigint :byte_size, null: false 9 t.string :checksum, null: false 10 t.datetime :created_at, null: false 11 12 t.index [ :key ], unique: true 13 end 14 15 create_table :active_storage_attachments do |t| 16 t.string :name, null: false 17 t.references :record, null: false, polymorphic: true, index: false 18 t.references :blob, null: false 19 20 t.datetime :created_at, null: false 21 22 t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true 23 t.foreign_key :active_storage_blobs, column: :blob_id 24 end 25 end 26end
config/storage.yml
1amazon: 2 service: S3 3 access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> 4 secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> 5 region: <%= Rails.application.credentials.dig(:aws, :s3, :region) %> 6 bucket: <%= Rails.application.credentials.dig(:aws, :s3, :bucket) %>
app/models/message.rb
1class Message < ApplicationRecord 2(省略) 3 has_one_attached :image 4end
app/views/rooms/show.html.erb
1<div class="row my-5"> 2(省略) 3 <% if @messages.present? %> 4(省略) 5<!-- 画像を表示するコード --> 6 <%= image_tag @message.image.variant(resize:'500x500'), class: 'message-image' if @message.image.attached? %> 7<!-- --> 8 9 <% else %> 10 <p>メッセージはまだありません。</p> 11 <% end %> 12 13 14 <%= form_with model: @message do |f| %> 15(省略) 16<!-- 画像を選択するコード --> 17 <div class="field"> 18 <%= f.file_field :images, direct_upload: true %> 19 </div> 20<!-- --> 21 22 <div class="form-group"> 23 <%= f.submit "送信", class: "btn btn-primary" %> 24 </div> 25 <% end %> 26</div> 27
messages_controller.rb
試したこと
以下記事を参考にしており、ビューのコードに問題があるかと
複数の記事を参考にして変えてみましたが、解決に至っていません。
https://qiita.com/tsubasan1122/items/0171fe04754a760f7e4a
https://shangang7321.hatenablog.com/entry/2020/09/04/093647
補足情報(FW/ツールのバージョンなど)
ruby '3.0.2'
rails ' 6.0.4'
追記(20220408)
以下記事を参考にS3の公開設定を行いましたが、解決には至っていません。
https://qiita.com/dayjournal/items/c827a17917127bff3906
・ブロックパブリックアクセスをオフに設定
・バケットポリシーを以下に編集
{ "Version": "2012-10-17", "Statement": [ { "Sid": "Statement1", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::バケット名/*" } ] }
###追記(20220410)
開発ツールでの検証です。
そもそも画像を取得できていない状況です。
回答1件