LoginSignup
3
2

More than 5 years have passed since last update.

rails5にしたらAWSのS3に画像をアップできなくなった際の治し方【undefined method `match' 】

Posted at

rails5で画像アップすると undefined method `match'

エラー内容

エラー分は以下の通りである。

undefined method `match' for nil:NilClass excluded from capture: Not configured to send/capture in environment 'development'

NoMethodError (undefined method `match' for nil:NilClass):

app/controllers/admins/attachments_controller.rb:37:in `upload_eyecatch'

原因

これらの記事を読んだところ AWS REGION を明示的に記入する必要があることがわかった。

https://stackoverflow.com/questions/37129020/aws-s3-integration-yields-undefined-method-match
https://github.com/ankane/pghero/issues/69
https://code.i-harness.com/ja/q/2368b3c

解決方法

環境変数 AWS_REGION を定義する

.env
AWS_REGION="ap-hogehoge-1"

こちらはお好みで Setting.aws.aws_region とアクセスしたい場合に設定ください。

development.yml
aws:
  alias: <%= ENV['AWS_ALIAS'] %>
  access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  region: <%= ENV['AWS_REGION'] %>
使用部分にこれを
  s3_option_private = {
    storage: :s3,
    url: ":s3_alias_url",
    s3_protocol: :https,
    s3_host_alias: Settings.aws.alias,
    s3_permissions: :private,
    s3_credentials: {
      bucket: Settings.aws.bucket,
      access_key_id: Settings.aws.access_key_id,
      secret_access_key: Settings.aws.secret_access_key
    },
    s3_region: Settings.aws.region #ここを追記する
  }

これで対応完了。

解決しない場合

rails c で環境変数が入っているかチェック

rails_c
Settings.aws.region
3
2
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
3
2