学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails6.1 | 仕事売買アプリ作成 | 15 | Amazon S3

↓↓クリックして頂けると励みになります。



14 | Active Storageの利用】 << 【ホーム】 >> 【16 | カラムの追加


Amazon S3の利用
Amazon S3の利用


Amazon S3(Simple Storage Service)は、Amazon Web Services(AWS)のクラウドストレージサービスの1つです。
S3は、データの保存、バックアップ、アーカイブ、データの配信など、さまざまなデータストレージ関連の用途に使用されます。


herokuにアプリケーションをデプロイすると画像をローカルに保存することができません。
それに対応するため、アップロードした画像を全てアマゾンS3に保存するよう設定します。


アマゾンS3のキーの取得方法は以下の手順でお願いします。
mrradiology.hatenablog.jp


GemFileに以下の記述を追加して下さい。


記述追加 GemFile(64行目)

gem "aws-sdk"



GemFile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.1.2'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.7', '>= 6.1.7.6'
# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 4.1.0'
  # Display performance information such as SQL time and flame graphs for each request in your browser.
  # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
  gem 'rack-mini-profiler', '~> 2.0'
  gem 'listen', '~> 3.3'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 3.26'
  gem 'selenium-webdriver', '>= 4.0.0.rc1'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

# device
gem 'devise', '~> 4.8', '>= 4.8.1'

# 日本語化
gem 'rails-i18n'

# Amazon S3
gem "aws-sdk"



コマンド
bundle


「config\environments\development.rb」ファイルの記述を更新します。


記述更新 config\environments\development.rb
34行目の「config.active_storage.service = :local」の記述を「:amazon」に変更します。

  # config.active_storage.service = :local
  config.active_storage.service = :amazon



「config\storage.yml」ファイルに以下の記述を追加します。


記述追加 config\storage.yml(9行目)
アクセスキー、シークレットキー、バケット名はご自分のものを入れて下さい。

amazon:
  service: S3
  access_key_id: 'ご自分のアクセスキーを入れてください。'
  secret_access_key: 'ご自分のシークレットアクセスキーを入れてください。'
  region: "ap-northeast-1"
  bucket: "ご自分のバケット名を入れてください。"



config\storage.yml

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

amazon:
  service: S3
  access_key_id: 'ご自分のアクセスキーを入れてください。'
  secret_access_key: 'ご自分のシークレットアクセスキーを入れてください。'
  region: "ap-northeast-1"
  bucket: "ご自分のバケット名を入れてください。"

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
# amazon:
#   service: S3
#   access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
#   secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
#   region: us-east-1
#   bucket: your_own_bucket

# Remember not to checkin your GCS keyfile to a repository
# google:
#   service: GCS
#   project: your_project
#   credentials: <%= Rails.root.join("path/to/gcs.keyfile") %>
#   bucket: your_own_bucket

# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key)
# microsoft:
#   service: AzureStorage
#   storage_account_name: your_account_name
#   storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %>
#   container: your_container_name

# mirror:
#   service: Mirror
#   primary: local
#   mirrors: [ amazon, google, microsoft ]



ブラウザ確認
http://localhost:3000/dashboard


画像をアップロードすると画像が表示されます。

PCレイアウト
PCレイアウト



アマゾンS3のバケットを見て画像がアップロードされているか確認して下さい。

画像アップロード成功
画像アップロード成功


14 | Active Storageの利用】 << 【ホーム】 >> 【16 | カラムの追加


↓↓クリックして頂けると励みになります。