LoginSignup
36
20

More than 5 years have passed since last update.

AWS S3で「Access Denied」を解決する

Posted at

はまっていたところ

Rails + Carrierwave + fog + S3で画像アップロード機能をつくっていたところ、「Access Denied」にはまる。
永遠に抜け出せないのではないかと思いながらバケットポリシーに挑んでいたところを、
エラーAccess Denied 〜Rails + Carrierwave + HerokuでAWS S3に画像を保存〜
こちらの記事に救っていただきました。

ただ、またここでも1つ落とし穴?があり、かなりの時間を使ってしまったので、その解決方法を残しておきます。

前提条件

  • rails側、Carrierwave、fogの設定
  • s3でのバケット作成
  • IAM関連、ユーザーへの権限付与
  • バケットポリシーの設定
  • 「Access Denied」の解決

ここの設定方法は先ほどもあげた下記の記事等で触れられているので、まだの場合はこちらを参考にお願いします。
(エラーAccess Denied 〜Rails + Carrierwave + HerokuでAWS S3に画像を保存〜)

解決方法

s3.png

s3_acount.png

アカウント+バケット のパブリックアクセス設定を画像のように変更する。

バケットのパブリックアクセス設定のみを変更するだけで終わると、引き続き「Access Denied」の海で溺れることになります。(体験談)

解決しなかったら

  • rails側、Carrierwave、fogの設定
  • IAM関連、ユーザーへの権限付与
  • バケットポリシーの設定

このあたりの設定を1から見直してみてください。

おまけ

実はもうひとつ詰まっていたところがあり、

carriervave.rb

require 'carrierwave/storage/abstract'
require 'carrierwave/storage/file'
require 'carrierwave/storage/fog'

CarrierWave.configure do |config|
  # if Rails.env.production?
  # これしか書いていない状態でstaging環境でテストし、「ArgumentError ( is not a recognized provider):」
  if Rails.env.production? || Rails.env.staging?
    config.storage :fog
    config.fog_provider = 'fog/aws'
    config.fog_directory  = 'xxxxxxxxxxxxxxxx'
    config.asset_host = 'https://s3-ap-northeast-1.amazonaws.com/xxxxxxxxxxxxxxxx'
    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: "s3-ap-northeast-1",
      path_style: true
    }
  else
    config.storage :file
    config.enable_processing = false if Rails.env.test?
  end
end

CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/

if Rails.env.production?
これしか書いていない状態でstaging環境でテストし、ArgumentError ( is not a recognized provider):を出し続けるという間抜けなことをやっていました。

36
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
20